Java Swing简单计算器无法正确添加

时间:2014-03-30 04:38:39

标签: java swing

我发现这个代码用于一个简单的Java Swing计算器,并在Eclipse中运行它,但它没有正确添加数字,我不知道为什么。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 A simplified calculator.
 The only operations are addition and subtraction
 */
public class Calculator extends JFrame 
                        implements ActionListener 
{
    public static final int WIDTH = 400;
    public static final int HEIGHT = 200;
    public static final int NUMBER_OF_DIGITS = 30;
    private JTextField ioField;
    private double result = 0.0;

    public static void main (String[] args)
    {
        Calculator aCalculator = new Calculator();
        aCalculator.setVisible(true);
    }

    public Calculator() 
    {
        setTitle("Simplified Calculator");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(WIDTH, HEIGHT);
        setLayout(new BorderLayout());

        JPanel textPanel = new JPanel();
        textPanel.setLayout(new FlowLayout());
        ioField = 
                new JTextField("Enter numbers here.", NUMBER_OF_DIGITS);
        ioField.setBackground(Color.WHITE);
        textPanel.add(ioField);
        add(textPanel, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setBackground(Color.BLUE);
        buttonPanel.setLayout(new FlowLayout());

        JButton addButton = new JButton("+");
        addButton.addActionListener(this);
        buttonPanel.add(addButton);

        JButton subtractButton = new JButton("-");
        addButton.addActionListener(this);
        buttonPanel.add(subtractButton);

        JButton resetButton = new JButton("Reset");
        resetButton.addActionListener(this);
        buttonPanel.add(resetButton);

        add(buttonPanel, BorderLayout.CENTER);
    }
    public void actionPerformed(ActionEvent e)
    {
        try
        {
            assumingCorrectNumberFormats(e);
        }
        catch (NumberFormatException e2)
        {
            ioField.setText("Error: Reenter Number.");
        }
    }
    //Throws NumberFormatException.
    public void assumingCorrectNumberFormats(ActionEvent e)
    {
        String actionCommand = e.getActionCommand();
        if (actionCommand.equals("+"))
        {
            result = result + stringToDouble(ioField.getText());
            ioField.setText(Double.toString(result));
        }
        else if (actionCommand.equals("-"))
        {
            result = result - stringToDouble(ioField.getText());
            ioField.setText(Double.toString(result));
        }
        else if (actionCommand.equals("Reset"))
        {
            result = 0.0;
            ioField.setText("0.0");
        }
        else
            ioField.setText("Unexpected error.");
    }
    //Throw NumberFormatException.
    private static double stringToDouble(String stringObject)
    {
        return Double.parseDouble(stringObject.trim());
    }
}

我的猜测是assumingCorrectNumberFormats方法存在问题,因为那是actionPerformed中实际处理事件监听器的问题,但我看不出那里存在缺陷。

当我运行程序时实际发生的事情:如果我输入10然后加号,文本框会立即输出20,就像我已经隐式输入10一样。如果我反复按加号,则文本框中的数字每次乘以4。

2 个答案:

答案 0 :(得分:3)

我在你的程序中发现了一个错误。 请检查这些代码:

 JButton subtractButton = new JButton("-");
        addButton.addActionListener(this);
        buttonPanel.add(subtractButton);

在第二行中,addButton应该是subtractButton。

答案 1 :(得分:3)

这不是你的代码错误,系统获得两次键盘命令。

    if (actionCommand.equals("+"))
    {
        System.out.println(ioField.getText());
        result = result + stringToDouble(ioField.getText());
        ioField.setText(Double.toString(result));
    }

尝试使用您的代码,您就会明白这一点。