liv*_*ove 8 java swing layout-manager

我想做一个看起来像上面的gui.现在我有一个面板,它将保存名称标签,名称文本字段,brith日期标签和生日文本字段.我的问题是什么是最好的布局管理器在面板上使用,以便"名称组件"行(标签+文本字段)和"出生日期组件"行(标签+文本字段),将垂直均匀地分布在小组.
我想过使用流布局,但这会导致两行组件之间没有间隙.我想过使用网格布局,但我不知道两行组件之间的间隙大小.
一个更复杂的方法......我考虑将名称标签和文本字段放在一个面板中,并将出生日期标签和文本字段放在另一个面板中,然后使基本面板边框布局和设置名称为北,生日日期为南. ..但是我仍然需要确保名称组件在名称面板中垂直居中,而birthdate组件在出生日期面板中垂直居中.
任何帮助表示赞赏.目标是确保名称组件行和出生日期组件行垂直展开,名称组件垂直居中于上半部分,出生日期组件垂直居中于下半部分.如果有什么问题让人感到困惑,请告诉我我会尝试改写以便更好地理解.
我正在使用严格的Java swing与eclipse,没有GUI构建器或类似的东西.
我喜欢MigLayout.在这种情况下,布局组件将非常容易,因为MigLayout具有类似于表的行为,并且组件将以这种方式排列.这在QuickStartGuide中有所描述.
编辑:
这是一个小例子:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class Test {
private JFrame frame;
private JTextField nameTextField, birthDateTextField;
private JLabel nameLabel, birthDateLabel;
public Test() {
initComponents();
}
public static void main(String args[]) {
new Test();
}
private void initComponents() {
frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new MigLayout());
nameTextField = new JTextField(30);
birthDateTextField = new JTextField(30);
nameLabel = new JLabel("Name:");
birthDateLabel = new JLabel("Birth Date:");
frame.add(nameLabel);
frame.add(nameTextField, "wrap");
frame.add(birthDateLabel);
frame.add(birthDateTextField);
frame.pack();
frame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
我个人喜欢GridBagLayout。
下面是一个例子来演示:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
public class Test {
private JFrame frame;
private JTextField nameTextField, birthDateTextField;
private JLabel nameLabel, birthDateLabel;
public Test() {
initComponents();
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
new Test();
}
});
}
private void initComponents() {
frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
nameTextField = new JTextField(30);
birthDateTextField = new JTextField(30);
nameLabel = new JLabel("Name:");
birthDateLabel = new JLabel("Birth Date:");
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.HORIZONTAL;
gc.insets = new Insets(10, 10, 10, 10);
gc.gridx = 0;
gc.gridy = 0;
frame.add(nameLabel, gc);
gc.gridx = 1;
gc.gridy = 0;
frame.add(nameTextField, gc);
gc.gridx = 0;
gc.gridy = 1;
frame.add(birthDateLabel, gc);
gc.gridx = 1;
gc.gridy = 1;
frame.add(birthDateTextField, gc);
frame.pack();
frame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)