IllegalStateException Error. While outlaying compo

2019-08-28 05:41发布

New to GUI in java, I am trying out GroupLayouts to layout graphical components, I am doing something wrong, but no Idea what. I can't find what's causing this error. Help is desperately needed.

Exception in thread "main" Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException:

at javax.swing.GroupLayout.checkComponents(Unknown Source)
at javax.swing.GroupLayout.prepare(Unknown Source)
at javax.swing.GroupLayout.layoutContainer(Unknown Source)
at java.awt.Container.layout(Unknown Source)
at java.awt.Container.doLayout(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validate(Unknown Source)
at java.awt.Container.validateUnconditionally(Unknown Source)
at java.awt.Window.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at AlignTest.main(AlignTest.java:12)

java.lang.IllegalStateException:

This is the code:

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;enter code here
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class AlignFrame extends JFrame {


    private JTextField xTField;
    private JTextField yTField;

    private JLabel xLabel;
    private JLabel yLabel;

    private JCheckBox sToGr;
    private JCheckBox showGrid;

    private JButton okButton;
    private JButton cancelButton;
    private JButton helpButton;

    public AlignFrame()
    {
        super("Align");
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);

        xLabel = new JLabel("X: ");
        yLabel = new JLabel("Y: ");

        xTField = new JTextField("");
        yTField = new JTextField("");

        sToGr= new JCheckBox("Snap to Grid");
        showGrid = new JCheckBox("Show Grid");

        okButton = new JButton("OK");
        cancelButton = new JButton("Cancel");
        helpButton = new JButton("Help");

        layout.setAutoCreateContainerGaps(true);
        layout.setAutoCreateGaps(true);

        layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addComponent(sToGr)
                        .addComponent(showGrid)
                        )
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                        .addComponent(xLabel)
                                        .addComponent(yLabel))
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                        .addComponent(xTField)
                                        .addComponent(yTField))
                                )
                        )
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addComponent(okButton)
                        .addComponent(cancelButton)
                        .addComponent(helpButton))
                );

        layout.setVerticalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addComponent(sToGr)
                        .addComponent(showGrid)
                        )
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addComponent(xLabel)
                        .addComponent(yLabel))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)                     
                        .addComponent(okButton)
                        .addComponent(cancelButton)
                        .addComponent(helpButton))

                );
    }

}


####################################################################
############### AlignTest class that contains main #################
####################################################################

import javax.swing.JFrame;


public class AlignTest {

    public static void main(String [] args)
    {
        AlignFrame test = new AlignFrame();

        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test.setSize(500, 500);
        test.setVisible(true);


    }

}

1条回答
Summer. ? 凉城
2楼-- · 2019-08-28 06:21

The actual exception contains the relevant information:

java.lang.IllegalStateException: javax.swing.JTextField[...lotsa garbage here...] is not attached to a vertical group

You are calling

layout.setHorizontalGroup(...)

and

layout.setVerticalGroup(...)

But in the latter, the JTextFields (xTField and yTField) are not appearing. Although this can be "fixed" by inserting these lines...

...
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    .addComponent(xLabel)
    .addComponent(yLabel)
    .addComponent(xTField) // <---
    .addComponent(yTField) // <---
)

in the vertical group, I'd strongly recommend you to have a look at http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html . Maybe I'm just not a great fan of "Visual GUI editors", but I think you should only use them when you can be sure that nobody ever has to look at the code again. (This rarely happens - draw your conclusions...)

查看更多
登录 后发表回答