对齐一组元件与Swing布局管理器我想创建一个Swing形式。我怎么能在中心春季
的形式应类似于此:
____________________________________
|___________________________________X|
| |
| ________________ |
| label1 [________________] |
| ________________ |
| label2 [________________] |
| |
| |
| [Save] [Close] |
|____________________________________|
但我无法定心文本字段垂直。
下面是用于打开帧代码:
public class Main {
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
LayoutTest test = new LayoutTest();
test.setVisible(true);
}
});
}
}
这里是草签的字段和创建布局代码:
public class LayoutTest {
private JFrame testFrame;
JLabel label1;
JTextField field1;
JLabel label2;
JTextField field2;
JButton saveButton;
JButton closeButton;
public LayoutTest() {
testFrame = new JFrame();
initProperties();
initContent();
initLayout();
}
private void initProperties() {
testFrame.setTitle("Test");
testFrame.setSize(1000, 800);
testFrame.setLocationRelativeTo(null);
testFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private void initContent() {
label1= new JLabel("Label 1");
field1= new JTextField(25);
label1.setLabelFor(field1);
label2= new JLabel("Label 2");
field2= new JTextField(25);
label2.setLabelFor(field2);
saveButton = new JButton("Save");
closeButton = new JButton("Close");
closeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
testFrame.add(label1);
testFrame.add(field1);
testFrame.add(label2);
testFrame.add(field2);
testFrame.add(saveButton);
testFrame.add(closeButton);
}
private void initLayout() {
SpringLayout layout = new SpringLayout();
layout.putConstraint(SpringLayout.NORTH, label1, 10, SpringLayout.NORTH, testFrame.getContentPane());
layout.putConstraint(SpringLayout.EAST, label1, -5, SpringLayout.WEST, field1);
layout.putConstraint(SpringLayout.NORTH, field1, 10, SpringLayout.NORTH, testFrame.getContentPane());
layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, field1, -10, SpringLayout.HORIZONTAL_CENTER, testFrame.getContentPane());
layout.putConstraint(SpringLayout.NORTH, label2, 10, SpringLayout.SOUTH, label1);
layout.putConstraint(SpringLayout.EAST, label2, -5, SpringLayout.WEST, field2);
layout.putConstraint(SpringLayout.NORTH, field2, 5, SpringLayout.SOUTH, field1);
layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, field2, -10, SpringLayout.HORIZONTAL_CENTER, testFrame.getContentPane());
layout.putConstraint(SpringLayout.SOUTH, closeButton, -20, SpringLayout.SOUTH, testFrame.getContentPane());
layout.putConstraint(SpringLayout.EAST, closeButton, -20, SpringLayout.EAST, testFrame.getContentPane());
layout.putConstraint(SpringLayout.SOUTH, saveButton, -20, SpringLayout.SOUTH, testFrame.getContentPane());
layout.putConstraint(SpringLayout.EAST, saveButton, -10, SpringLayout.WEST, closeButton);
testFrame.setLayout(layout);
}
public void setVisible(boolean visible) {
testFrame.setVisible(visible);
}
}
我试图将这些字段添加到然后在面板上使用SpringLayout.VERTICAL_CENTER
,但没有显示出来。
我怎么能中心对齐等领域作为一个群体?
你有使用' SpringLayout'? – MadProgrammer
如果您可以建议一种简单的方法来创建我需要的表单而不需要添加太多额外的组件(例如面板等),那么我很乐意切换。 'SpringLayout'似乎是最灵活的,这是我无法用它做的唯一的事情。 –
SpringLayout(与之后的GroupLayout相同)设计用于构建器,而不是手动构建。所以我会投资学习一些其他经理的怪癖,我个人目前的偏好是MigLayout – kleopatra