我在设置布局之前初始化了MotePanel、Command Panel和LEDPanel,为什么会出现这个异常呢?
请帮忙解决。
请帮忙解决。
Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
at javax.swing.BoxLayout.checkContainer(BoxLayout.java:462)
at javax.swing.BoxLayout.invalidateLayout(BoxLayout.java:246)
at javax.swing.BoxLayout.addLayoutComponent(BoxLayout.java:279)
at java.awt.Container.addImpl(Container.java:1107)
at java.awt.Container.add(Container.java:974)
at javax.swing.JFrame.addImpl(JFrame.java:556)
at java.awt.Container.add(Container.java:377)
at Window.<init>(Window.java:54)
public class Window extends JFrame{
private JPanel MotePanel;
private JPanel LEDPanel;
private JPanel CommandPanel;
private JCheckBox motes[];
private JRadioButton Leds[];
public Window(){
this.setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
this.setTitle("Sensor Networks Lab");
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
MotePanel = new JPanel();
LEDPanel = new JPanel();
CommandPanel = new JPanel();
motes = new JCheckBox[10];
Leds = new JRadioButton[3];
MotePanel.setLayout(new BoxLayout(MotePanel, BoxLayout.Y_AXIS));
CommandPanel.setLayout(new BoxLayout(CommandPanel, BoxLayout.Y_AXIS));
LEDPanel.setLayout(new BoxLayout(LEDPanel, BoxLayout.Y_AXIS));
System.out.println("creating MotePanel");
for(int i=0; i<10; i++){
motes[i] = new JCheckBox("Mote "+i);
MotePanel.add(motes[i]);
}
System.out.println("creating LEDPanel");
for(int i=0; i<3; i++)
Leds[i] = new JRadioButton();
Leds[0].setText("RED");
LEDPanel.add(Leds[0]);
Leds[1].setText("GREEN");
LEDPanel.add(Leds[1]);
Leds[2].setText("BLUE");
LEDPanel.add(Leds[2]);
this.add(MotePanel);
this.add(LEDPanel);
this.add(CommandPanel);
}