你好,我对此有问题。我正在尝试创建2个面板。一个面板的标签在BoxLayout中堆叠在一起,另一个面板具有TextFields,也具有与标签相对应的BoxLayout。我尝试了各种设置,但不断出现错误。
我正在将JFrame上的Layout设置为FlowLayout()
并使用此设置面板布局管理器
leftPanel = new JPanel(new BoxLayout(this, BoxLayout.Y_AXIS));
这是我以前毫无疑问地做过的事情。现在怎么了?
错误:
Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
at javax.swing.BoxLayout.checkContainer(BoxLayout.java:465)
at javax.swing.BoxLayout.invalidateLayout(BoxLayout.java:249)
at javax.swing.BoxLayout.addLayoutComponent(BoxLayout.java:282)
at java.awt.Container.addImpl(Container.java:1125)
at java.awt.Container.add(Container.java:415)
at DataWriteExample.BuildLeftPanel(DataWriteExample.java:37)
at DataWriteExample.<init>(DataWriteExample.java:24)
at DataWriteExample.main(DataWriteExample.java:58)
Java Result: 1
您可以试试这个:
leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
更新
你的代码问题是:
leftPanel = new JPanel(new BoxLayout(this, BoxLayout.Y_AXIS));
-------------------------------------^
// here `this` is not leftPanel, I believe its the JFrame instance
// hence your getting the exception of cannot share the layout