我的面板使用GroupLayout
:
GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup()
.addComponent(title_panel)
.addComponent(subtitle_panel)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addComponent(title_panel)
.addComponent(subtitle_panel)
);
即使我定义了水平组和垂直组,它仍然会给我一个
java.lang.IllegalStateException
但并非总是如此。有时它做到了,有时却没有。即使出现这种异常,我的GUI也能正常工作并显示我想要的内容。
我想我已经有了答案。当我第一次显示面板所在的框架时,我没有这个例外。但是,当我第二次显示它时,它给了我这个例外。这就像程序在框架的两次启动之间保留了我的groupLayout的一部分,或者类似的东西。
这很令人沮丧,即使它能工作,尽管有这个例外^^'我不喜欢我自己的代码攻击我!
以下是我得到的错误示例(derp是我项目的名称,Panel是从JPanel继承的类):
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: derp.Panel[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=] is not attached to a horizontal group
at javax.swing.GroupLayout.checkComponents(GroupLayout.java:1086)
at javax.swing.GroupLayout.prepare(GroupLayout.java:1040)
at javax.swing.GroupLayout.layoutContainer(GroupLayout.java:910)
at java.awt.Container.layout(Container.java:1503)
at java.awt.Container.doLayout(Container.java:1492)
at java.awt.Container.validateTree(Container.java:1688)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validate(Container.java:1623)
at javax.swing.RepaintManager$2.run(RepaintManager.java:679)
at javax.swing.RepaintManager$2.run(RepaintManager.java:677)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.java:676)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1650)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:100)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
juin 27, 2013 4:39:35 PM io.socket.IOConnection transportMessage
我认为问题来自这两行代码,"this"指的是类的实例,GroupLayout的父级必须是Swing容器,例如
GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
试试这个
JComponent panel = ...;
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);