さて、あなたの説明は少し紛らわしいです(または私は今日まだ疲れすぎているか、まだ十分なカフェインを持っていませんでした)。他の人からパネルクラスを「呼び出す」というあなたの概念も少し奇妙です。
しかし、私が見る限り、あなたの最初の選択肢は正しいものです。
一般に、実行時にオブジェクトをネストするだけなので、次のようになります。
InputPanel (has BorderLayout)
+--DetailsPanel (put in BorderLayout.WEST; has GridLayout)
| +--nameLabel
| +--nameTextField
| +--...
+--CrimePanel (put in BorderLayout.NORTH; has GridLayout)
| +--murderRadioButton
| +--arsonRadioButton
| +--...
+--ButtonPanel (put in BorderLayout.CENTER; has GridLayout)
+--button
通常、これは適切なクラスのコンストラクターで行います。
public class InputPanel {
public InputPanel() {
this.setLayout(new BorderLayout());
this.add(new DetailsPanel(), BorderLayout.WEST);
this.add(new CrimePanel(), BorderLayout.NORTH);
this.add(new ButtonPanel(), BorderLayout.CENTER);
}
}
public class DetailsPanel {
JLabel nameLabel;
JTextField nameField;
// ...
public DetailsPanel() {
this.setLayout(new GridLayout(5, 1));
nameLabel = new JLabel("Name");
nameField = new JTextField();
// ...
this.add(nameLabel);
this.add(nameField);
// ...
}
}
...
ただし、ここに小さな問題があります。GridLayout
コンポーネントが複数の列にまたがることができないためDetailsPanel
、左側の他のパネルもネストする必要がある場合があります。必要な機能を備えたシングルで逃げることができますGridBagLayout
。または、他のパネルをそこにネストすることもできます。
DetailsPanel (has BorderLayout)
+--panel1 (has GridLayout with 2 rows, 1 column; put in BorderLayout.NORTH)
| +--nameLabel
| +--nameField
+--panel2 (has GridLayout with 3 rows, 2 columns; put in BorderLayout.CENTER)
+--dayField
+--dayLabel
+--monthField
+--...