我正在编写一个自定义JPanel,手动调用它MyPanel,它包含两个JLabels和两个JTextBoxes。此JPanel将用于包含使用CardLayout的其他JPanel的JFrame。 JFrame不可调整大小;其大小基于组件最多的JPanel来确定。填充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可以做到这一点,我完全可以这么做)。
我可能有一些古怪的伎俩,我不知道,但这就是我的方式,除了我会使用透明的组件,以防万一 – MadProgrammer
*“称之为MyPanel”*为什么不称它为明智的?为了更快地获得更好的帮助,请发布[SSCCE](http://sscce.org/)。 –
@AndrewThompson嗯,它实际上叫做FindCardsByYearAndNumberPanel,但我不想每次都输入它= p –