使用GUI时出现错误:IllegalStateException

时间:2018-11-15 21:22:31

标签: java swing layout-manager illegalstateexception grouplayout

即使在Net Bean中制作程序时,我尝试运行程序时仍会出现错误,但没有错误。只有当我尝试编译时,才会弹出50个错误。有人解释说,我的JTextField未附加到Horizo​​ntal组,我以为我已修复它,但似乎没有。

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)

谁能告诉我我要去哪里错了?

1 个答案:

答案 0 :(得分:0)

Yo Briana,

我没有足够的时间来解决所有问题,但是您的两个最大问题包括不向垂直组中添加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
}