GroupLayout中的IllegalStateException [英] IllegalStateException in a GroupLayout

查看:101
本文介绍了GroupLayout中的IllegalStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在面板上使用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)
    );

即使我同时定义了水平组和垂直组,它仍然会给我一个

Even if I define both horizontal AND vertical groups, it still gives me a

java.lang.IllegalStateException

但并非总是如此.有时会这样做,有时却不会.即使有这种例外情况,我的GUI仍能正常工作并显示我想要的内容.

But not always. Sometimes it does it, sometimes it does not. And even if there is this exception, my GUI works and displays what I want.

我想我已经开始回答了.当我第一次显示面板所在的框架时,我没有这个异常.但是,当我第二次显示它时,它给了我这个例外.就像程序在框架的两次启动之间保留了我的groupLayout的一部分一样.

I think I have a start of answer. When I display the frame on which the panel is for the first time, I do not have this exception. But, when I display it for the second time, it gives me this exception. It's like the program keeps a part of my groupLayout, or something like that, between two launchs of the frame.

即使有这种例外情况,它仍然可以使人沮丧,^^'我不喜欢自己的代码拖拉我!

It's quite frustrating, even if it works in spite of this exception ^^' I do not like when my own code trolls me!

这是我得到的错误的一个示例(derp是我的projet的名称,Panel是从JPanel继承的类):

Here is an example of the errors I get (derp is the name of my projet and Panel is a class inherited from 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容器,例如一个面板

I think the problem comes from these two line of code, "this" refers to the instance of your class, the parent of a GroupLayout must be a Swing container, a panel for example

GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);

尝试这样的事情

JComponent panel = ...;
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);

这篇关于GroupLayout中的IllegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆