GroupLayout错误:在我的代码中找不到错误的行

时间:2014-10-19 16:40:05

标签: java swing layout-manager grouplayout

我正在尝试创建一个GroupLayout,但每次编译时都会出错。我在代码中找不到错误的行,在我看来是正确的。为什么我会收到这些错误?

构造函数中的代码(扩展JFrame):

mem_settings = new JPanel();

    lbl_mem_settings = new JLabel("Memory settings");
    lbl_mem_select = new JLabel("Type");
    cb_mem_select = new JComboBox(new String[] {"24C01","24C02","24C04"});


    // Layout
    GroupLayout GL = new GroupLayout(mem_settings);
    mem_settings.setLayout(GL);
    GL.setAutoCreateGaps(true);
    GL.setAutoCreateContainerGaps(true);

    GL.setHorizontalGroup(GL.createSequentialGroup()
        .addGroup(GL.createParallelGroup(LEADING)
            .addComponent(lbl_mem_settings)
            .addComponent(lbl_mem_select))
        .addGroup(GL.createParallelGroup(LEADING)
            .addComponent(cb_mem_select))
    );
    GL.setVerticalGroup(GL.createSequentialGroup()
        .addGroup(GL.createParallelGroup(BASELINE)
            .addComponent(lbl_mem_settings))
        .addGroup(GL.createParallelGroup(BASELINE)
            .addComponent(lbl_mem_settings)
            .addComponent(cb_mem_select))  
    );

    add(mem_settings);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setVisible(true);

我得到的错误:

run:
Exception in thread "main" 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=Type,verticalAlignment=CENTER,verticalTextPosition=CENTER] 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.preferredLayoutSize(GroupLayout.java:878)
    at java.awt.Container.preferredSize(Container.java:1794)
    at java.awt.Container.getPreferredSize(Container.java:1778)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1661)
    at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:719)
    at java.awt.Container.preferredSize(Container.java:1794)
    at java.awt.Container.getPreferredSize(Container.java:1778)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1661)
    at javax.swing.JRootPane$RootLayout.preferredLayoutSize(JRootPane.java:920)
    at java.awt.Container.preferredSize(Container.java:1794)
    at java.awt.Container.getPreferredSize(Container.java:1778)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1661)
    at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:719)
    at java.awt.Container.preferredSize(Container.java:1794)
    at java.awt.Container.getPreferredSize(Container.java:1778)
    at java.awt.Window.pack(Window.java:809)
    at memcryp.GUI.<init>(GUI.java:46)
    at memcryp.MEMCRYP.main(MEMCRYP.java:20)
BUILD STOPPED (total time: 6 minutes 39 seconds)

1 个答案:

答案 0 :(得分:1)

但错误信息很明显。您JLabel名为lbl_mem_select的{​​{1}}仅附加到水平组:

GL.setHorizontalGroup(GL.createSequentialGroup()
        .addGroup(GL.createParallelGroup(LEADING)
            .addComponent(lbl_mem_settings)
            .addComponent(lbl_mem_select)) //here you attach the label
        .addGroup(GL.createParallelGroup(LEADING)
            .addComponent(cb_mem_select))
    );

但是,在您的垂直组中,您没有包含标签。将垂直组的代码更改为

GL.setVerticalGroup(GL.createSequentialGroup()
        .addGroup(GL.createParallelGroup(BASELINE)
            .addComponent(lbl_mem_settings)
            .addComponent(lbl_mem_select)) //add this line
        .addGroup(GL.createParallelGroup(BASELINE)
            .addComponent(lbl_mem_settings)
            .addComponent(cb_mem_select))  
    );

应该这样做。

话虽如此......你真的想用GroupLayout手工编写UI代码。来自GroupLayout教程

的引用
  

GroupLayout是为GUI构建器开发的布局管理器,例如Matisse,随NetBeans IDE提供的GUI构建器

虽然可以用它手工编写布局代码,但它需要相当多的(不可读的imho)代码。还有其他(第三方)布局管理器更适合手动编码布局。