java - 我的 Java Swing Designer 计算器无法运行。 (循环的等于按钮按下时会出错)

标签 java swing user-interface awt calculator

package calculator;

import java.awt.*;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.Collections;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

import com.jgoodies.forms.factories.DefaultComponentFactory;

public class CalculatorWindow {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CalculatorWindow window = new CalculatorWindow();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public CalculatorWindow() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {

        frame = new JFrame();
        frame.getContentPane().setBackground(Color.LIGHT_GRAY);
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        final JLabel outputBox = new JLabel("");
        outputBox.setBackground(Color.WHITE);
        outputBox.setForeground(Color.WHITE);
        outputBox.setBounds(12, 39, 424, 112);
        frame.getContentPane().add(outputBox);

        JButton one = new JButton("1");
        one.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                outputBox.setText(outputBox.getText() + "1");
            }
        });
        one.setBounds(12, 163, 55, 25);
        frame.getContentPane().add(one);

        JButton two = new JButton("2");
        two.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                outputBox.setText(outputBox.getText() + "2");
            }
        });
        two.setBounds(77, 163, 55, 25);
        frame.getContentPane().add(two);

        JButton three = new JButton("3");
        three.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                outputBox.setText(outputBox.getText() + "3");
            }
        });
        three.setBounds(144, 163, 55, 25);
        frame.getContentPane().add(three);

        JButton four = new JButton("4");
        four.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                outputBox.setText(outputBox.getText() + "4");
            }
        });
        four.setBounds(12, 200, 55, 25);
        frame.getContentPane().add(four);

        JButton five = new JButton("5");
        five.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                outputBox.setText(outputBox.getText() + "5");
            }
        });
        five.setBounds(77, 200, 55, 25);
        frame.getContentPane().add(five);

        JButton six = new JButton("6");
        six.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                outputBox.setText(outputBox.getText() + "6");
            }
        });
        six.setBounds(144, 200, 55, 25);
        frame.getContentPane().add(six);

        JButton seven = new JButton("7");
        seven.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                outputBox.setText(outputBox.getText() + "7");
            }
        });
        seven.setBounds(12, 237, 55, 25);
        frame.getContentPane().add(seven);

        JButton eight = new JButton("8");
        eight.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                outputBox.setText(outputBox.getText() + "8");
            }
        });
        eight.setBounds(77, 237, 55, 25);
        frame.getContentPane().add(eight);

        JButton nine = new JButton("9");
        nine.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                outputBox.setText(outputBox.getText() + "9");
            }
        });
        nine.setBounds(144, 237, 55, 25);
        frame.getContentPane().add(nine);

        JButton zero = new JButton("0");
        zero.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                outputBox.setText(outputBox.getText() + "0");
            }
        });
        zero.setBounds(207, 237, 55, 25);
        frame.getContentPane().add(zero);

        JButton add = new JButton("+");
        add.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                outputBox.setText(outputBox.getText() + " + ");
            }
        });
        add.setBounds(207, 200, 55, 25);
        frame.getContentPane().add(add);

        JButton subtract = new JButton("-");
        subtract.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                outputBox.setText(outputBox.getText() + " - ");
            }
        });
        subtract.setBounds(207, 163, 55, 25);
        frame.getContentPane().add(subtract);

        JButton multiply = new JButton("x");
        multiply.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                outputBox.setText(outputBox.getText() + " x ");
            }
        });
        multiply.setBounds(274, 163, 55, 25);
        frame.getContentPane().add(multiply);

        JButton divide = new JButton("÷");
        divide.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                outputBox.setText(outputBox.getText() + " ÷ ");
            }
        });
        divide.setBounds(274, 200, 55, 25);
        frame.getContentPane().add(divide);

        JButton equals = new JButton("=");
        equals.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String[] outputBoxButtonsPressed = outputBox.getText().split(
                        " ");
                int answer;

                for (int buttonPressed = 0; buttonPressed < outputBoxButtonsPressed.length; buttonPressed++) {
                    if (outputBoxButtonsPressed[buttonPressed + 1].equals("+")) {
                        answer = Integer
                                .parseInt(outputBoxButtonsPressed[buttonPressed])
                                + Integer
                                        .parseInt(outputBoxButtonsPressed[buttonPressed + 2]);
                        break;
                    }
                }
            }
        });
        equals.setBounds(274, 237, 55, 25);
        frame.getContentPane().add(equals);

        JLabel lbTitle = DefaultComponentFactory.getInstance().createTitle(
                "Calculator - Coded by William Bryant (gogobebe2)");
        lbTitle.setFont(new Font("Andale Mono", Font.BOLD, 12));
        lbTitle.setBounds(12, 12, 424, 15);
        frame.getContentPane().add(lbTitle);

        JLabel img = new JLabel("");
        Image theImage = new ImageIcon(this.getClass().getResource(
                "/calculatorIcon.png")).getImage();
        img.setIcon(new ImageIcon(theImage));
        img.setBounds(336, 163, 100, 106);
        frame.getContentPane().add(img);

    }
}

我认为这是 for 循环中 equals 按钮处理程序的 if 语句中的内容 这会导致错误。

这是当我按下按钮“3”、“3”、“+”、“6”、“6”(在计算器输出框中看起来为 33 + 66)时出现的错误消息:

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)

非常感谢任何帮助。我是 swing、awt 和 java 的新手。

最佳答案

这个循环有问题:

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

具体来说,当 buttonPressed 时,注释行上会发生错误。 > = outputBoxButtonsPressed.length - 2因为您要访问数组最多超出其结束位置的两个元素。您可以通过将循环条件更改为buttonPressed < outputBoxButtonsPressed.length - 2来防止错误。 ,但我不会起诉这是否会阻止您的代码工作。

关于java - 我的 Java Swing Designer 计算器无法运行。 (循环的等于按钮按下时会出错),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27073828/

相关文章:

java - 从年月 (yyyy-MM) 到年月 (yyyy-MMMM)

java - 如何使用 JOptionPane 和 showMessage Dialog 显示自动垂直滚动条?

java - 退出框架时如何关闭我的程序

user-interface - 图表加载时显示 HighCharts 工具提示

java - 带选项卡的 JFace 对话框

java - 输入无效后继续提示用户

java - 部署到 Google App Engine 时出现“类文件是 Java 8,但最大支持的是 Java 7”错误

java - 如何从 jinternalframe 调用 jdialog

linux - 我可以嵌入到我的应用程序中的优秀轻量级 GUI 库是什么?

java - Hibernate 不连接 MySql