你能帮我理解一下这是怎么回事吗?我咨询了Javadoc: JFrame有setLayout方法。所以,对我来说,什么分享错误是个谜。
public class View extends JFrame {
public View(){
// LayoutManager for the whole frame.
this.setLayout(new BoxLayout(this, BoxLayout.X_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 java.awt.Container.invalidate(Container.java:1583)
at java.awt.Component.invalidateIfValid(Component.java:2957)
at java.awt.Container.setLayout(Container.java:1484)
at javax.swing.JFrame.setLayout(JFrame.java:605)
at View.<init>(View.java:16)
at Init.main(Init.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)发布于 2014-07-19 13:22:49
在JFrame#getContentPane()上试试这个
this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));阅读更多如何使用BoxLayout
所有组件都添加到JFrame's内容窗格中。
阅读更多向内容窗格中添加组件
下面是JFrame的图形表示形式

编辑
来自评论:
反正也不清楚。我这样分析它: BoxLayout类需要知道它的目标。JFrame有setLayoutt方法,需要知道它的布局。
this.setLayout(manager)内部调用getContentPane().setLayout(manager);
下一行
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));转换为不正确的下一行。
this.getContentPane().setLayout(new BoxLayout(this, BoxLayout.X_AXIS));有关更多细节,请看一看源代码
https://stackoverflow.com/questions/24840860
复制相似问题