简体   繁体   中英

GroupLayout error: can't find the wrong line in my code

I'm trying to make a GroupLayout, but every time I compile I get errors. I can't find the wrong line in my code, it seems to me to be correct. Why do I get these errors?

The code in the constructor (extends 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);

and the error I get:

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)

The error message is clear though. Your JLabel called lbl_mem_select is only attached to a horizontal group:

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

However, in your vertical group you did not include the label. Changing the code for the vertical group to

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

should do it.

That being said ... do you really want to use the GroupLayout to hand-code a UI. A quote from the GroupLayout tutorial

GroupLayout is a layout manager that was developed for GUI builders such as Matisse, the GUI builder provided with the NetBeans IDE

While it is possible to hand-code a layout with it, it requires quite a lot of (unreadable imho) code to do it. There are other (third party) layout managers which are better suited for handcoding a layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM