Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的Java Swing Designer计算器赢得了';不行。(按下“循环”的“等于”按钮时出错)_Java_Swing_User Interface_Awt_Calculator - Fatal编程技术网

我的Java Swing Designer计算器赢得了';不行。(按下“循环”的“等于”按钮时出错)

我的Java Swing Designer计算器赢得了';不行。(按下“循环”的“等于”按钮时出错),java,swing,user-interface,awt,calculator,Java,Swing,User Interface,Awt,Calculator,非常感谢您的帮助。我不熟悉swing、awt和java。这个循环有问题: Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3 at calculator.CalculatorWindow$16.actionPerformed(CalculatorWindow.java:198) at javax.swing.AbstractButton.fireActionPerfo

非常感谢您的帮助。我不熟悉swing、awt和java。

这个循环有问题:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3
    at calculator.CalculatorWindow$16.actionPerformed(CalculatorWindow.java:198)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6516)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6281)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4872)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
for(int buttonPressed=0;buttonPressed
具体地说,当
buttonPressed
>=
outputboxbuttonpressed.length-2
访问数组时,注释行上会出现错误,因为您访问的数组最多超过其结束位置两个元素。您可以通过将循环条件更改为
buttonPressed
来防止出现错误,但如果这会阻止代码工作或不工作,我不介意。

public void actionPerformed(ActionEvent e){
for (int buttonPressed = 0; buttonPressed < outputBoxButtonsPressed.length; buttonPressed++) {
    if (outputBoxButtonsPressed[buttonPressed + 1].equals("+")) {
        answer = Integer
            .parseInt(outputBoxButtonsPressed[buttonPressed])
             + Integer
                 .parseInt(outputBoxButtonsPressed[buttonPressed + 2]); //here
        break;
    }
 }
字符串[]outputBoxButtonsPressed=outputBox.getText().split( " "); System.out.println(Arrays.toString(outputBoxButtonsPressed)); int-answer=0; 布尔值firstOperatorBeenUsed=false; 对于(int buttonPressed=0;buttonPressed

这个新的equals按钮处理程序可以完美地处理所有按钮。耶:D

这是最直接不过的了
ArrayIndexOutOfBoundsException
表示试图访问不存在的数组元素。place System.out.println(Arrays.toString(outputBoxButtonsPressed));在你第一次分配它的地方,我取出for循环,在actionPerformed()中调试它,然后键入2+1,打印结果是[2,+,1],就像它应该的那样。我不明白为什么它试图访问一个不存在的数组元素??非常感谢。:)我真的很感谢你的帮助。
for (int buttonPressed = 0; buttonPressed < outputBoxButtonsPressed.length; buttonPressed++) {
    if (outputBoxButtonsPressed[buttonPressed + 1].equals("+")) {
        answer = Integer
            .parseInt(outputBoxButtonsPressed[buttonPressed])
             + Integer
                 .parseInt(outputBoxButtonsPressed[buttonPressed + 2]); //here
        break;
    }
 }
public void actionPerformed(ActionEvent e) {
    String[] outputBoxButtonsPressed = outputBox.getText().split(
            " ");
    System.out.println(Arrays.toString(outputBoxButtonsPressed));
    int answer = 0;
    boolean firstOperatorBeenUsed = false;

    for (int buttonPressed = 0; buttonPressed < outputBoxButtonsPressed.length - 2; buttonPressed++) {
        if (outputBoxButtonsPressed[buttonPressed + 1].equals("+")
                && !firstOperatorBeenUsed) {
            answer = answer
                    + Integer
                            .parseInt(outputBoxButtonsPressed[buttonPressed])
                    + Integer
                            .parseInt(outputBoxButtonsPressed[buttonPressed + 2]);
            System.out.println(answer);
            firstOperatorBeenUsed = true;
        } else if (outputBoxButtonsPressed[buttonPressed + 1]
                .equals("+") && firstOperatorBeenUsed) {
            answer = answer
                    + Integer
                            .parseInt(outputBoxButtonsPressed[buttonPressed + 2]);

        }
        if (outputBoxButtonsPressed[buttonPressed + 1].equals("-")
                && !firstOperatorBeenUsed) {
            answer = answer
                    + Integer
                            .parseInt(outputBoxButtonsPressed[buttonPressed])
                    - Integer
                            .parseInt(outputBoxButtonsPressed[buttonPressed + 2]);
            System.out.println(answer);
            firstOperatorBeenUsed = true;
        } else if (outputBoxButtonsPressed[buttonPressed + 1]
                .equals("-") && firstOperatorBeenUsed) {
            answer = answer
                    - Integer
                            .parseInt(outputBoxButtonsPressed[buttonPressed + 2]);

        }

        if (outputBoxButtonsPressed[buttonPressed + 1].equals("x")
                && !firstOperatorBeenUsed) {
            answer = answer
                    + Integer
                            .parseInt(outputBoxButtonsPressed[buttonPressed])
                    * Integer
                            .parseInt(outputBoxButtonsPressed[buttonPressed + 2]);
            System.out.println(answer);
            firstOperatorBeenUsed = true;
        } else if (outputBoxButtonsPressed[buttonPressed + 1]
                .equals("x") && firstOperatorBeenUsed) {
            answer = answer
                    * Integer
                            .parseInt(outputBoxButtonsPressed[buttonPressed + 2]);

        }

        if (outputBoxButtonsPressed[buttonPressed + 1].equals("÷")
                && !firstOperatorBeenUsed) {
            answer = answer
                    + Integer
                            .parseInt(outputBoxButtonsPressed[buttonPressed])
                    / Integer
                            .parseInt(outputBoxButtonsPressed[buttonPressed + 2]);
            System.out.println(answer);
            firstOperatorBeenUsed = true;
        } else if (outputBoxButtonsPressed[buttonPressed + 1]
                .equals("÷") && firstOperatorBeenUsed) {
            answer = answer
                    / Integer
                            .parseInt(outputBoxButtonsPressed[buttonPressed + 2]);

        }

    }
    outputBox.setText(outputBox.getText() + " = " + answer);
}