使用 GUI 时的错误:IllegalStateException

Posted

技术标签:

【中文标题】使用 GUI 时的错误:IllegalStateException【英文标题】:Errors when using GUI: IllegalStateException 【发布时间】:2019-04-19 01:33:31 【问题描述】:

我在尝试运行我的程序时不断出错,即使在使用 net beans 制作程序时我没有任何错误。只有当我尝试编译时才会弹出 50 个错误。有人解释说我的JTextField 没有附加到水平组,我以为我修复了它,但似乎没有。

public class TrianglePerimeter extends javax.swing.JFrame 

public TrianglePerimeter() 
    utilizeComponents();


@SuppressWarnings("Not Checked")
private void utilizeComponents() 
    sideOneInput = new javax.swing.JTextField();
    sideTwoInput = new javax.swing.JTextField();
    sideThreeInput = new javax.swing.JTextField();
    sideOneLabel = new javax.swing.JLabel();
    sideTwoLabel = new javax.swing.JLabel();
    sideThreeLabel = new javax.swing.JLabel();
    findPerimeterButton = new javax.swing.JButton();
    perimeterLabel = new javax.swing.JLabel();
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Perimeter of a Triangle");
    findPerimeterButton.setText("Find the Perimeter");
    findPerimeterButton.addActionListener(new java.awt.event.ActionListener() 
        public void actionPerformed(java.awt.event.ActionEvent evt) 
            findPerimeterButtonActionPerformed(evt);
        
    
    );

    sideOneLabel.setText("Side One");
    sideTwoLabel.setText("Side Two");
    sideThreeLabel.setText("Side Three");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                            .addGap(29, 29, 29)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(findPerimeterButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(sideOneInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(sideTwoInput)
                                                    .addComponent(sideThreeInput)
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                            .addGroup(layout.createSequentialGroup()
                                                                    .addGap(16, 16, 16)
                                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                            .addComponent(sideOneLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                                                                            .addComponent(sideTwoLabel, javax.swing.GroupLayout.Alignment.TRAILING)))
                                                            .addGroup(layout.createSequentialGroup()
                                                                    .addGap(18, 18, 18)
                                                                    .addComponent(sideThreeLabel)))
                                                    .addGap(56, 56, 56)
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                            .addComponent(perimeterLabel)))))
                                    .addContainerGap(67, Short.MAX_VALUE))
                    );

    layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(sideOneInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(sideOneLabel))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(sideTwoInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(sideTwoLabel))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(sideThreeInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(sideThreeLabel))
                            .addGap(18, 18, 18)
                            .addComponent(findPerimeterButton)
                            .addContainerGap(27, Short.MAX_VALUE)
                    ));
    pack();


private void findPerimeterButtonActionPerformed(java.awt.event.ActionEvent evt) 
    System.out.println(evt);
    try 
        double tempPerimeter = (Double.parseDouble(sideOneInput.getText())) + (Double.parseDouble(sideTwoInput.getText())) + (Double.parseDouble(sideThreeInput.getText()));
        perimeterLabel.setText(tempPerimeter + " is the perimeter");
     catch (NumberFormatException e) 
        System.err.println("NumberFormatException: " + e.getMessage());
    


public static void main(String args[]) 
    java.awt.EventQueue.invokeLater(() -> 
        new TrianglePerimeter().setVisible(true);
    );


// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel sideOneLabel;
private javax.swing.JLabel sideTwoLabel;
private javax.swing.JLabel sideThreeLabel;
private javax.swing.JButton findPerimeterButton;
private javax.swing.JLabel perimeterLabel;
private javax.swing.JTextField sideOneInput;
private javax.swing.JTextField sideTwoInput;
private javax.swing.JTextField sideThreeInput;
private javax.swing.JTextField tempTextField;
// End of variables declaration//GEN-END:variables

这些是错误

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@3566d06c,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING] is not attached to a horizontal group
at javax.swing.GroupLayout.checkComponents(GroupLayout.java:1086)
at javax.swing.GroupLayout.prepare(GroupLayout.java:1040)
at javax.swing.GroupLayout.preferredLayoutSize(GroupLayout.java:878)
at java.awt.Container.preferredSize(Container.java:1799)
at java.awt.Container.getPreferredSize(Container.java:1783)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1664)
at javax.swing.JRootPane$RootLayout.preferredLayoutSize(JRootPane.java:920)
at java.awt.Container.preferredSize(Container.java:1799)
at java.awt.Container.getPreferredSize(Container.java:1783)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1664)
at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:719)
at java.awt.Container.preferredSize(Container.java:1799)
at java.awt.Container.getPreferredSize(Container.java:1783)
at java.awt.Window.pack(Window.java:809)
at TrianglePerimeter.TrianglePerimeter.utilizeComponents(TrianglePerimeter.java:96)
at TrianglePerimeter.TrianglePerimeter.<init>(TrianglePerimeter.java:16)
at TrianglePerimeter.TrianglePerimeter.lambda$main$0(TrianglePerimeter.java:111)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

谁能告诉我哪里出错了?

【问题讨论】:

啊,GroupLayout,你的代码很痛苦。 GroupLayout 是一个非常复杂的布局管理器,旨在供 GUI 编辑器使用,一般不手动编码。我会考虑使用不同的布局管理器,或者甚至改用 MigLayout 很遗憾,因为它的课程我们不能使用另一个布局管理器 然后扩展@MadProgrammer 的建议,如果GroupLayout 是不可协商的,使用GUI 编辑器可以为自己节省很多压力和理智。顺便说一句 - 可悲的是,这门课程的讲师似乎不知道他们在做什么.. 【参考方案1】:

哟布里安娜,

我没有足够的时间来解决所有问题,但您的两个最大问题包括未将 perimeterLabel 组件添加到垂直组以及使用并行组开始每个布局。如果您阅读tutorial on group layout from oracle,您会注意到每个布局都以SequentialGroup 开头,而不是并行组。那是因为您需要在引用并行元素之前定义布局中的元素序列。它类似于 2d 数组如何首先声明它包含的数组的数量,然后是每个数组中的元素。

无论如何,我没有添加周边标签,以便您可以查看答案并尝试解决它应该如何定位。我还删除了所有空白(但将您的代码留在那里注释以便您阅读它)。如果您将自动间隙设置为true,则会为您处理大小调整,它将做出响应。

欢迎提问。我认为这应该是解决问题的良好开端。

public class TrianglePerimeter extends javax.swing.JFrame 

    public TrianglePerimeter() 
        utilizeComponents();
    

    @SuppressWarnings("Not Checked")
    private void utilizeComponents() 
        sideOneInput = new javax.swing.JTextField();
        sideTwoInput = new javax.swing.JTextField();
        sideThreeInput = new javax.swing.JTextField();
        sideOneLabel = new javax.swing.JLabel();
        sideTwoLabel = new javax.swing.JLabel();
        sideThreeLabel = new javax.swing.JLabel();
        findPerimeterButton = new javax.swing.JButton();
        perimeterLabel = new javax.swing.JLabel();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Perimeter of a Triangle");
        findPerimeterButton.setText("Find the Perimeter");
        findPerimeterButton.addActionListener(new java.awt.event.ActionListener() 
                                                  public void actionPerformed(java.awt.event.ActionEvent evt) 
                                                      findPerimeterButtonActionPerformed(evt);
                                                  
                                              
        );

        sideOneLabel.setText("Side One");
        sideTwoLabel.setText("Side Two");
        sideThreeLabel.setText("Side Three");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        layout.setHorizontalGroup(
                        layout.createSequentialGroup()
//                                .addGap(29, 29, 29)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(layout.createSequentialGroup()
//                                                                .addGap(16, 16, 16)
                                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addComponent(sideOneLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                                                                        .addComponent(sideTwoLabel, javax.swing.GroupLayout.Alignment.TRAILING)))
                                                        .addGroup(layout.createSequentialGroup()
//                                                                .addGap(18, 18, 18)
                                                                .addComponent(sideThreeLabel)))
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(sideOneInput)
                                                        .addComponent(sideTwoInput)
                                                        .addComponent(sideThreeInput))

//                                                .addGap(56, 56, 56)
                                        )
                                        .addComponent(findPerimeterButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                                )
//                                .addContainerGap(67, Short.MAX_VALUE)
        );

        layout.setVerticalGroup(
                        layout.createSequentialGroup()
//                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(sideOneLabel)
                                        .addComponent(sideOneInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
//                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(sideTwoLabel)
                                        .addComponent(sideTwoInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
//                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(sideThreeLabel)
                                        .addComponent(sideThreeInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
//                                .addGap(18, 18, 18)
                                .addComponent(findPerimeterButton)
//                                .addContainerGap(27, Short.MAX_VALUE)
                        );
        pack();
    

    private void findPerimeterButtonActionPerformed(java.awt.event.ActionEvent evt) 
        System.out.println(evt);
        try 
            double tempPerimeter = (Double.parseDouble(sideOneInput.getText())) + (Double.parseDouble(sideTwoInput.getText())) + (Double.parseDouble(sideThreeInput.getText()));
            perimeterLabel.setText(tempPerimeter + " is the perimeter");
         catch (NumberFormatException e) 
            System.err.println("NumberFormatException: " + e.getMessage());
        
    

    public static void main(String args[]) 
        java.awt.EventQueue.invokeLater(() -> 
            new TrianglePerimeter().setVisible(true);
        );
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel sideOneLabel;
    private javax.swing.JLabel sideTwoLabel;
    private javax.swing.JLabel sideThreeLabel;
    private javax.swing.JButton findPerimeterButton;
    private javax.swing.JLabel perimeterLabel;
    private javax.swing.JTextField sideOneInput;
    private javax.swing.JTextField sideTwoInput;
    private javax.swing.JTextField sideThreeInput;
    private javax.swing.JTextField tempTextField;
// End of variables declaration//GEN-END:variables

【讨论】:

以上是关于使用 GUI 时的错误:IllegalStateException的主要内容,如果未能解决你的问题,请参考以下文章

Matlab GUI(axes中)如何获取鼠标点击时的位置

使用 pthreads 时的后台线程(很好,优先级)

Java:单击按钮时的 JDBC 连接问题

DELPHI调用DLL时的回调函数问题

无法发布具有许多(超过 256 个)值的表单

Liquibase - ZipFile.java 中的 FileNotFoundException