Java中的计算器,不工作

时间:2016-12-29 17:44:34

标签: java calculator calc

我有两个不同的班级。是的,他们在同一个项目,包装一切。这个项目来自Yakov Fain的书籍:儿童,父母和祖父母的Java编程。页数:95-100。您可以轻松获得该书的PDF。两个不同的班级'名字如下。为了我将展示我的两个班级。

计算器本身的类:计算器

运行计算器的类:CalculatorEngine

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;

public class Calculator {

JButton button0 = new JButton("0");
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton buttonPoint = new JButton(".");
JButton buttonEqual = new JButton("=");
JButton buttonPlus = new JButton("+");
JButton buttonSub = new JButton("-");
JButton buttonMult = new JButton("*");
JButton buttonDiv = new JButton("/");
JPanel windowContent = new JPanel();
JTextField displayField = new JTextField(30);

Calculator() {

    BorderLayout bl = new BorderLayout();
    windowContent.setLayout(bl);

    windowContent.add("North", displayField);

    JPanel p1 = new JPanel();
    GridLayout gl = new GridLayout(4, 3);
    p1.setLayout(gl);

    p1.add(button0);
    p1.add(button1);
    p1.add(button2);
    p1.add(button3);
    p1.add(button4);
    p1.add(button5);
    p1.add(button6);
    p1.add(button7);
    p1.add(button8);
    p1.add(button9);
    p1.add(buttonPoint);
    p1.add(buttonEqual);

    windowContent.add("Center", p1);

    JPanel p2 = new JPanel();
    GridLayout gl2 = new GridLayout(4, 1);
    p2.setLayout(gl2);
    p2.add(buttonDiv);
    p2.add(buttonMult);
    p2.add(buttonSub);
    p2.add(buttonPlus);

    windowContent.add("East", p2);

    JFrame frame = new JFrame("Calculator");
    frame.setContentPane(windowContent);

    frame.pack();
    frame.setVisible(true);

    CalculatorEngine calcEngine = new CalculatorEngine(this);

    button0.addActionListener(calcEngine);
    button1.addActionListener(calcEngine);
    button2.addActionListener(calcEngine);
    button3.addActionListener(calcEngine);
    button4.addActionListener(calcEngine);
    button5.addActionListener(calcEngine);
    button6.addActionListener(calcEngine);
    button7.addActionListener(calcEngine);
    button8.addActionListener(calcEngine);
    button9.addActionListener(calcEngine);
    buttonDiv.addActionListener(calcEngine);
    buttonMult.addActionListener(calcEngine);
    buttonSub.addActionListener(calcEngine);
    buttonPlus.addActionListener(calcEngine);
    buttonPoint.addActionListener(calcEngine);
    buttonEqual.addActionListener(calcEngine);
}

public static void main(String[] args) {
    Calculator calc = new Calculator();
}
}

*那是计算器类。

**现在,下面是运行Calculator类的代码。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

public class CalculatorEngine implements ActionListener {

Calculator parent;
char selectedAction = ' ';

double currentResult = 0;

CalculatorEngine(Calculator parent) {
    this.parent = parent;
}

public void actionPerformed(ActionEvent e) {

    JButton clickedButton = (JButton) e.getSource();
    String dispFieldText = parent.displayField.getText();

    double displayValue = 0;

    if (!"".equals(dispFieldText)) {
        displayValue = Double.parseDouble(dispFieldText);
    }
    Object src = e.getSource();

    if (src == parent.buttonPlus) {
        selectedAction = '+';
        currentResult = displayValue;
        parent.displayField.setText("");
    } else if (src == parent.buttonSub) {
        selectedAction = '-';
        currentResult = displayValue;
        parent.displayField.setText("");
    } else if (src == parent.buttonMult) {
        selectedAction = '*';
        currentResult = displayValue;
        parent.displayField.setText("");
    } else if (src == parent.buttonDiv) {
        selectedAction = '/';
        currentResult = displayValue;
        parent.displayField.setText("");

        if (selectedAction == '+') {
            currentResult += displayValue;
            parent.displayField.setText("" + currentResult);
        } else if (selectedAction == '-') {
            currentResult -= displayValue;
            parent.displayField.setText("" + currentResult);
        } else if (selectedAction == '*') {
            currentResult *= displayValue;
            parent.displayField.setText("" + currentResult);
        } else if (selectedAction == '/') {
            currentResult /= displayValue;
            parent.displayField.setText("" + currentResult);
        }
    } else {
        String clickedButtonLabel = clickedButton.getText();
        parent.displayField.setText(dispFieldText + clickedButtonLabel);
    }
}
}

这两个类向控制台显示一个如下所示的计算器: (https://i.stack.imgur.com/Y4XHE.png

每当我运行程序并输入一个等式:1 + 1,然后我按下' =',程序就不会运行而且没有给出在这种情况下,等式的结果为2。我也查看了书中的代码,它与我的想法完全相同或不同。

1 个答案:

答案 0 :(得分:0)

您忘记处理用户点击actionPerformed方法中的等号(=)按钮(CalculatorEngine.java内)的事件。将其更改为此并且有效:

public void actionPerformed(ActionEvent e) {

        JButton clickedButton = (JButton) e.getSource();
        String dispFieldText = parent.displayField.getText();

        double displayValue = 0;

        if (!"".equals(dispFieldText)) {
            displayValue = Double.parseDouble(dispFieldText);
        }
        Object src = e.getSource();

        if (src == parent.buttonPlus) {
            selectedAction = '+';
            currentResult = displayValue;
            parent.displayField.setText("");
        } else if (src == parent.buttonSub) {
            selectedAction = '-';
            currentResult = displayValue;
            parent.displayField.setText("");
        } else if (src == parent.buttonMult) {
            selectedAction = '*';
            currentResult = displayValue;
            parent.displayField.setText("");
        } else if (src == parent.buttonDiv) {
            selectedAction = '/';
            currentResult = displayValue;
            parent.displayField.setText("");
        } else if (src == parent.buttonEqual) {
            if (selectedAction == '+') {
                currentResult += displayValue;
                parent.displayField.setText("" + currentResult);
            } else if (selectedAction == '-') {
                currentResult -= displayValue;
                parent.displayField.setText("" + currentResult);
            } else if (selectedAction == '*') {
                currentResult *= displayValue;
                parent.displayField.setText("" + currentResult);
            } else if (selectedAction == '/') {
                currentResult /= displayValue;
                parent.displayField.setText("" + currentResult);
            }
        } else {
            String clickedButtonLabel = clickedButton.getText();
            parent.displayField.setText(dispFieldText + clickedButtonLabel);
        }
}