我想使用 GroupLayout 获取用户名标签/字段和密码标签/字段显示,如下所示:
:用户名字段
密码:密码字段
我使用的代码如下:
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)
为什么我会收到此错误,如何解决?
资源1:我将我的逻辑(即我用来决定哪个组应该是平行/顺序的网格逻辑)与其中的答案进行了比较,它似乎是正确的。
资源 2
看看这个:
.addGroup(layout.createSequentialGroup()
.addComponent(passwordLabel)
.addComponent(passwordLabel)
)
这看起来不对劲。 为什么要添加两次标签?
如果其中一个应该是passwordField
的,这可以解释为什么异常说有一个PasswordText
对象没有附加到垂直组。
JPasswordField ... is not attached to a vertical group
清楚地表明它不附加到垂直组。
你这里有错别字:
.addGroup(layout.createSequentialGroup()
.addComponent(passwordLabel)
.addComponent(passwordLabel)
)
使用组布局可能会很棘手。根据我的经验,我发现将第三方布局管理器用于 Swing 应用程序更容易,例如非常好的 MigLayout 它有大量的文档并且非常易于使用。试试吧。