2 つの JLabel と 2 つの JTextBox を含むカスタム JPanel を手動でコーディングしています。これを MyPanel と呼びます。この 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 でこれができるのであれば、私はそれで十分です。)