0
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);
}
}

我正在尝试运行它,但出现以下异常:

线程“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=,horizo​​ntalAlignment=LEADING,horizo​​ntalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=a,verticalAlignment=CENTER,verticalTextPosition=CENTER] 未附加到水平组

有什么问题?我该如何解决?

4

1 回答 1

3

您必须同时指定水平和垂直布局,另请参阅GroupLayout 给出 java swing 错误

我建议您使用一个工具来帮助您构建 GUI。

于 2013-05-02T20:53:36.160 回答