好吧,你的描述有点混乱(或者我今天还是太累了,或者还没有足够的咖啡因)。您从其他人那里“调用”面板类的概念也有点奇怪。
但据我所知,您的第一个选项是正确的。
一般来说,你只是在运行时嵌套对象,所以它可能看起来像下面这样:
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
+--...