java.awt.AWTError : BoxLayout can't be shared 错误

标签 java swing layout-manager

<分区>

我在设置布局之前已经初始化了 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);
    }

最佳答案

在 JFrame 上调用 setLayout 时,实际上是将布局添加到 JFrame 的 contentPane 而不是 JFrame 本身,因为此方法更像是一种将方法调用传输到 contentPane 的便捷方法。 BoxLayout 构造函数必须反射(reflect)这一点,因为您不能将 BoxLayout 添加到一个容器,然后作为参数传入另一个容器。所以改变这个:

this.setLayout(new BoxLayout(this,BoxLayout.X_AXIS));

为此:

setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));

此外:不需要所有 this. 业务,因为这。是隐含的,而且这里也没有实际需要扩展 JFrame。

编辑:下面的代码是证明您的错误及其解决方案所需的全部内容:

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 can't be shared 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5917017/

相关文章:

java - 邻接矩阵(java)中的邻居(int参数)遇到一些问题

java - 运行 jar 时找不到或加载主类

java - swing flowlayout preferredSize 不会自动改变

java - 如何动态更改 JFrame 内的 JPanel?

java - 如何在空布局中添加滚动条?

java - 读取 perl 脚本错误 : CreateProcess error=193, %1 不是有效的 Win32 应用程序

java - Java 桌面应用程序的 HSQLDB 有哪些限制?

java - MigLayout 使用 JScrollPane 调整大小

java - 事件发生时重绘功能不起作用

Java Swing 面板布局