问题描述
我在设置布局之前初始化了MotePanel,命令面板和LEDPanel,那么我是如何得到这个例外的。
I have initialized MotePanel, Command Panel and LEDPanel before setting their Layout, then how so am I getting this exception.
请帮忙。
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);
}
推荐答案
在JFrame上调用setLayout时,你实际上是将布局添加到JFrame的contentPane而不是JFrame本身,因为这种方法更像是一种将方法调用传递给contentPane的便捷方法。 BoxLayout构造函数必须反映这一点,因为您无法将BoxLayout添加到一个容器,然后作为参数传入另一个容器。所以改变这个:
When calling setLayout on a JFrame, you're actually adding the layout to the JFrame's contentPane not the JFrame itself since this method is more of a convenience method that transmits the method call to the contentPane. The BoxLayout constructor must reflect this since you can't add the BoxLayout to one container and then pass in as a parameter a different container. So change this:
this.setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
到此:
setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
另外:不需要所有这个。
此后的业务。暗示,并且此处也没有实际需要扩展JFrame。
Also: no need for all the this.
business since this. is implied, and also there is no actual need here to extend JFrame.
编辑:以下代码是演示错误及其解决方案所需的全部内容:
edit: this code below is all that is needed to demonstrate your error and its solution:
import javax.swing.*;
public class BoxLayoutFoo extends JFrame {
public BoxLayoutFoo() {
// swap the comments below
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS)); // comment out this line
//setLayout(new BoxLayout(getContentPane(), BoxLayout.LINE_AXIS)); // uncomment this line
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
new BoxLayoutFoo();
}
}
这篇关于java.awt.AWTError:无法共享BoxLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!