我正在编写一个自定义 JPanel,称为 MyPanel,手动包含两个 JLabel 和两个 JTextBox。这个 JPanel 将在一个 JFrame 中使用,该 JFrame 包含使用 CardLayout 的其他 JPanel。JFrame 不可调整大小;它的大小是基于组件最多的 JPanel 固定的。
由于 MyPanel 的控件很少,我试图找出最好的布局管理器来使用。我希望标签和文本字段显示在靠近顶部的位置,底部有所有额外的空间。我相信 GridBagLayout 提供了让这个面板看起来像我想要的方式的功能。MyPanel 具有以下从构造函数调用的 initComponents() 方法:
public class MyPanel extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("MyPanel Test");
frame.add(new MyPanel(null));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 425);
frame.setVisible(true);
}
public MyPanel() {
initComponents();
}
private void initComponents() {
this.setLayout(new GridBagLayout());
JLabel cardYearLabel = new JLabel("Card Year:");
cardYearLabel.setFont(new Font("Tahoma", 0, 14)); // NOI18N
GridBagConstraints yearLabelConstraints = new GridBagConstraints();
yearLabelConstraints.gridx = 0;
yearLabelConstraints.gridy = 0;
yearLabelConstraints.weightx = 1;
yearLabelConstraints.weighty = 1;
yearLabelConstraints.anchor = GridBagConstraints.WEST;
yearLabelConstraints.insets = new Insets(20, 50, 10, 10);
this.add(cardYearLabel, yearLabelConstraints);
this.yearTextField = new JFormattedTextField();
this.yearTextField.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(new DecimalFormat("#0"))));
this.yearTextField.setFont(new Font("Tahoma", 0, 14)); // NOI18N
this.yearTextField.setColumns(10);
this.yearTextField.addFocusListener(new UpdateInstructionsFocusListener("Enter card year."));
GridBagConstraints yearTextFieldConstraints = new GridBagConstraints();
yearTextFieldConstraints.gridx = 1;
yearTextFieldConstraints.gridy = 0;
yearTextFieldConstraints.weightx = 2;
yearTextFieldConstraints.weighty = 1;
yearTextFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
yearTextFieldConstraints.insets = new Insets(20, 10, 10, 50);
this.add(this.yearTextField, yearTextFieldConstraints);
JLabel cardNumberLabel = new JLabel("Card Number:");
cardNumberLabel.setFont(new Font("Tahoma", 0, 14)); // NOI18N
GridBagConstraints numberLabelConstraints = new GridBagConstraints();
numberLabelConstraints.gridx = 0;
numberLabelConstraints.gridy = 1;
numberLabelConstraints.weightx = 1;
numberLabelConstraints.weighty = 1;
numberLabelConstraints.anchor = GridBagConstraints.WEST;
numberLabelConstraints.insets = new Insets(10, 50, 0, 10);
this.add(cardNumberLabel, numberLabelConstraints);
this.numberTextField = new JFormattedTextField();
this.numberTextField.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(new DecimalFormat("#0"))));
this.numberTextField.setFont(new Font("Tahoma", 0, 14)); // NOI18N
this.numberTextField.setColumns(10);
this.numberTextField.addFocusListener(new UpdateInstructionsFocusListener("Enter card number."));
GridBagConstraints numberTextFieldConstraints = new GridBagConstraints();
numberTextFieldConstraints.gridx = 1;
numberTextFieldConstraints.gridy = 1;
numberTextFieldConstraints.weightx = 2;
numberTextFieldConstraints.weighty = 1;
numberTextFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
numberTextFieldConstraints.insets = new Insets(10, 10, 0, 50);
this.add(this.numberTextField, numberTextFieldConstraints);
JPanel filler = new JPanel();
GridBagConstraints fillerConstraints = new GridBagConstraints();
fillerConstraints.gridx = 0;
fillerConstraints.gridy = 2;
fillerConstraints.gridwidth = 2;
fillerConstraints.weightx = 1;
fillerConstraints.weighty = 100;
this.add(filler, fillerConstraints);
}
}
我对使用空白的 JPanel 来填充 MyPanel 底部的空白空间感到畏缩。有没有更好、更优雅的方法来做到这一点?(我没有坚持使用 GridBagLayout。如果另一个标准 LayoutManager 可以做到这一点,我完全赞成。)