使用 GroupLayout 在 Java 中构建表单的最简单方法是什么?对于表单,我的意思是前面带有标签的文本字段。像这样的东西:
4 回答
使用Group Layout,您可以执行以下操作:
package foo;
import javax.swing.*;
import java.awt.*;
public class ChangeIpSettingsDialog extends JDialog
{
public ChangeIpSettingsDialog( Frame owner )
{
super( owner, true );
setContentPane( createContent() );
}
private Container createContent()
{
JPanel result = new JPanel();
result.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );
// Create the layout
GroupLayout layout = new GroupLayout( result );
result.setLayout( layout );
layout.setAutoCreateGaps( true );
// Create the components we will put in the form
JLabel ipAddressLabel = new JLabel( "IP Address:" );
JTextField ipAddressTextField = new JTextField( 20 );
JLabel subnetLabel = new JLabel( "Subnet:" );
JTextField subnetTextField = new JTextField( 20 );
JLabel gatewayLabel = new JLabel( "Gateway:" );
JTextField gatewayTextField = new JTextField( 20 );
// Horizontally, we want to align the labels and the text fields
// along the left (LEADING) edge
layout.setHorizontalGroup( layout.createSequentialGroup()
.addGroup( layout.createParallelGroup( GroupLayout.Alignment.LEADING )
.addComponent( ipAddressLabel )
.addComponent( subnetLabel )
.addComponent( gatewayLabel ) )
.addGroup( layout.createParallelGroup( GroupLayout.Alignment.LEADING )
.addComponent( ipAddressTextField )
.addComponent( subnetTextField )
.addComponent( gatewayTextField ) )
);
// Vertically, we want to align each label with his textfield
// on the baseline of the components
layout.setVerticalGroup( layout.createSequentialGroup()
.addGroup( layout.createParallelGroup( GroupLayout.Alignment.BASELINE )
.addComponent( ipAddressLabel )
.addComponent( ipAddressTextField ) )
.addGroup( layout.createParallelGroup( GroupLayout.Alignment.BASELINE )
.addComponent( subnetLabel )
.addComponent( subnetTextField ) )
.addGroup( layout.createParallelGroup( GroupLayout.Alignment.BASELINE )
.addComponent( gatewayLabel )
.addComponent( gatewayTextField ) )
);
return result;
}
public static void main( String[] args )
{
ChangeIpSettingsDialog dialog = new ChangeIpSettingsDialog( null );
dialog.pack();
dialog.setVisible( true );
}
}
或者您放弃GroupLayout
并使用FormLayout
,它主要设计为...表单的布局:-)
只需使用 NetBeans 附带的 GUI 编辑器,称为 Matisse。这是我见过的最神奇的 GUI 编辑器。它工作得非常好,你设计的所有窗口都可以调整大小。
此编辑器使用 GroupLayout 生成代码。
Matisse 的克隆也可用作 Eclipse 插件,但我认为它不是免费的。看看这里(免责声明:我以前从未使用过这个插件,所以我无法判断它是否与原始 Matisse 质量相同)
http://marketplace.eclipse.org/content/swing-gui-designer
这是一个不错的屏幕截图:
一个如何实现演示布局的示例GridBagLayout
:
class Main extends JFrame implements Runnable {
JLabel lblIpAddress = new JLabel();
JLabel lblSubnet = new JLabel();
JLabel lblGateway = new JLabel();
JTextField txtIpAddress = new JTextField();
JTextField txtSubnet = new JTextField();
JTextField txtGateway = new JTextField();
public void run() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = this.getContentPane();
lblIpAddress.setText("IP Address");
lblIpAddress.setLabelFor(txtIpAddress);
lblSubnet.setText("Subnet");
lblSubnet.setLabelFor(txtSubnet);
lblGateway.setText("Gateway");
lblGateway.setLabelFor(txtGateway);
GridBagLayout layout = new GridBagLayout();
content.setLayout(layout);
content.add(lblIpAddress, newLabelConstraints());
content.add(txtIpAddress, newTextFieldConstraints());
content.add(lblSubnet, newLabelConstraints());
content.add(txtSubnet, newTextFieldConstraints());
content.add(lblGateway, newLabelConstraints());
content.add(txtGateway, newTextFieldConstraints());
// Add a spacer to push all the form rows to the top of the window.
GridBagConstraints spacer = new GridBagConstraints();
spacer.fill=BOTH;
spacer.gridwidth=REMAINDER;
content.add(new JPanel(), spacer);
// make sure you can't "cut off" the controls when making the window smaller
this.pack();
this.setMinimumSize(this.getSize());
this.setVisible(true);
}
private GridBagConstraints newConstraints() {
GridBagConstraints c = new GridBagConstraints();
// a little breathing room
c.insets = new Insets(2, 2, 2, 2);
return c;
}
private GridBagConstraints newLabelConstraints() {
GridBagConstraints c = newConstraints();
// right-align labels
c.anchor = BASELINE_TRAILING;
// do not grow labels
c.weightx=0.0;
return c;
}
private GridBagConstraints newTextFieldConstraints() {
GridBagConstraints c = newConstraints();
c.anchor = BASELINE;
// grow text fields horizontally
c.weightx=1.0;
c.fill=HORIZONTAL;
// text fields end a row
c.gridwidth=REMAINDER;
return c;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Main());
}
}
主要的缺点是,如果你想说,在底部添加一排右对齐的按钮(例如:“确定”和“取消”),这些按钮与其他任何东西都不对齐,你会有使用嵌套的 JPanel。(或者做一些事情,比如让表单为每个按钮都有一个单独的列;然后让文本字段跨越所有这些列和一个额外的间隔列。这是相当违反直觉的,并且会否定可读性优势。我相信 MiGLayout,这是第三个-party 基于网格的布局管理器可以巧妙地处理这种情况,因为它允许合并/跨越网格单元格,并拆分合并的单元格。)