Java GroupLayout Exception

风流意气都作罢 提交于 2019-12-12 03:00:04

问题


public class Grouplayout implements Runnable {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Grouplayout());
}

@Override
public void run() {
    JFrame jFrame = new JFrame();
    GroupLayout layout = new GroupLayout(jFrame.getContentPane());
    jFrame.getContentPane().setLayout(layout);
    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);

    JLabel jLabel1 = new JLabel("a");
    JLabel jLabel2 = new JLabel("b");
    JLabel jLabel3 = new JLabel("c");
    JLabel jLabel4 = new JLabel("d");

    layout.setVerticalGroup(
        layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(jLabel2))
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(jLabel4)));

    jFrame.pack();
    jFrame.setVisible(true);
}
}

I'm trying to run it but i've got the following exception:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=a,verticalAlignment=CENTER,verticalTextPosition=CENTER] is not attached to a horizontal group

What's the problem? How can I fix it?


回答1:


You have to specify both a horizontal and a vertical layout, also see GroupLayout giving error with java swing

I suggest you use a tool to help you build the GUI.



来源:https://stackoverflow.com/questions/16347394/java-grouplayout-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!