我正在编写一个自定义JPanel,手动调用MyPanel,它包含两个JLabel和两个JTextBox。此JPanel将用于包含使用CardLayout的其他JPanel的JFrame。 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可以做到这一点,我会全力以赴。)
答案 0 :(得分:3)
实现此目的的一种方法是将控件放在面板中,然后使用PAGE_START
将该面板添加到第二个面板的BorderLayout
。