为什么我的GroupLayout会出现“主要异常”错误?

时间:2017-01-26 01:41:16

标签: java grouplayout


我想使用GroupLayout来获取usernameLabel / Field和passwordLabel / Field显示,如下所示:

用户名: usernameField
密码: passwordField

我使用的代码如下:

layout.setHorizontalGroup(
            layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(usernameLabel)
                .addComponent(usernameField) 
                    )
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel)
                    .addComponent(passwordField)
                    )
            );

        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(usernameLabel)
                    .addComponent(usernameField)
                     )
                .addGroup(layout.createSequentialGroup()
                    .addComponent(passwordLabel)
                    .addComponent(passwordLabel)
                    )
            );

这会编译,但会给我以下错误:

Exception in thread "main" java.lang.IllegalStateException: javax.swing.JPasswordField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=com.apple.laf.AquaTextFieldBorder@5910e440,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResource[r=128,g=128,b=128],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=com.apple.laf.AquaImageFactory$SystemColorProxy[r=0,g=0,b=0],selectionColor=com.apple.laf.AquaImageFactory$SystemColorProxy[r=164,g=205,b=255],columns=20,columnWidth=0,command=,horizontalAlignment=LEADING,echoChar=●] is not attached to a vertical group
    at javax.swing.GroupLayout.checkComponents(GroupLayout.java:1090)
    at javax.swing.GroupLayout.prepare(GroupLayout.java:1040)
    at javax.swing.GroupLayout.layoutContainer(GroupLayout.java:910)
    at java.awt.Container.layout(Container.java:1510)
    at java.awt.Container.doLayout(Container.java:1499)
    at java.awt.Container.validateTree(Container.java:1695)
    at java.awt.Container.validateTree(Container.java:1704)
    at java.awt.Container.validateTree(Container.java:1704)
    at java.awt.Container.validateTree(Container.java:1704)
    at java.awt.Container.validateTree(Container.java:1704)
    at java.awt.Container.validate(Container.java:1630)
    at java.awt.Container.validateUnconditionally(Container.java:1667)
    at java.awt.Window.show(Window.java:1033)
    at java.awt.Component.show(Component.java:1671)
    at java.awt.Component.setVisible(Component.java:1623)
    at java.awt.Window.setVisible(Window.java:1014)
    at xWing.GUIlogin.<init>(GUIlogin.java:107)
    at xWing.Driver.main(Driver.java:7)
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JPasswordField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=com.apple.laf.AquaTextFieldBorder@5910e440,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResource[r=128,g=128,b=128],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=com.apple.laf.AquaImageFactory$SystemColorProxy[r=0,g=0,b=0],selectionColor=com.apple.laf.AquaImageFactory$SystemColorProxy[r=164,g=205,b=255],columns=20,columnWidth=0,command=,horizontalAlignment=LEADING,echoChar=●] is not attached to a vertical group
    at javax.swing.GroupLayout.checkComponents(GroupLayout.java:1090)
    at javax.swing.GroupLayout.prepare(GroupLayout.java:1040)
    at javax.swing.GroupLayout.layoutContainer(GroupLayout.java:910)
    at java.awt.Container.layout(Container.java:1510)
    at java.awt.Container.doLayout(Container.java:1499)
    at java.awt.Container.validateTree(Container.java:1695)
    at java.awt.Container.validateTree(Container.java:1704)
    at java.awt.Container.validateTree(Container.java:1704)
    at java.awt.Container.validateTree(Container.java:1704)
    at java.awt.Container.validateTree(Container.java:1704)
    at java.awt.Container.validate(Container.java:1630)
    at java.awt.Window.dispatchEventImpl(Window.java:2744)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

为什么我会收到此错误,我该如何解决?

Resource 1:我将我的逻辑(即我用来决定哪个组应该是并行/顺序的网格逻辑)与此答案进行比较,似乎是正确的。
Resource 2

2 个答案:

答案 0 :(得分:1)

看看这个:

library(limma)
my_genes <- matrix(rnorm(9000, -10, 10), ncol=4)
my_genes <- as.data.frame(my_genes)
rownames(my_genes) <- paste("Gene", 1:nrow(my_genes))
## Randomly introducing NAs
purrr::map_df(my_genes, function(x) {x[sample(c(TRUE, NA), prob = c(0.95, 0.05), size = length(x), replace = TRUE)]})
tx <- 1:2 #suppose treatment is columns 1 & 2
ctrls <- 3:4 #suppose controls is columns 3 & 4
my_design <- model.matrix( ~factor(c(1,1,0,0)))
my_design
fit <- lmFit(my_genes, my_design)
fit <- eBayes(fit)
plot(fit$logFC, -log10(fit$p.value))

看起来不对劲。为什么要添加标签两次?

如果其中一个应该是 .addGroup(layout.createSequentialGroup() .addComponent(passwordLabel) .addComponent(passwordLabel) ) ,那么这可以解释为什么异常表明有一个passwordField对象未附加到垂直组。

答案 1 :(得分:1)

JPasswordField ... is not attached to a vertical group清楚地表明它不附加到垂直组。

你有错字:

        .addGroup(layout.createSequentialGroup()
            .addComponent(passwordLabel)
            .addComponent(passwordLabel)
            )

使用组布局可能很棘手。根据我的经验,我发现使用第三方布局管理器更容易Swing应用程序,如非常好MigLayout它有大量的文档,并且非常易于使用。尝试一下。