java - 使用 MVC java 计算器出现一堆错误

标签 java swing calculator

它与这个值有关; numOverall = (Integer.toString(theModel.getCalculationValue()));

我认为它在类(class)之间没有正确转移?

计算类

/* 
 * calc view class
 */



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


class Calc extends JFrame implements ActionListener {



    private JButton plusButton = new JButton("+");
    private JButton minusButton = new JButton("-");
    private JButton clearButton = new JButton("Clear");
    private JButton equalsButton = new JButton("=");
    private JButton zeroButton = new JButton("0");
    private JButton oneButton = new JButton("1");
    private JButton twoButton = new JButton("2");
    private JButton threeButton = new JButton("3");
    private JButton fourButton = new JButton("4");
    private JButton fiveButton = new JButton("5");
    private JButton sixButton = new JButton("6");
    private JButton sevenButton = new JButton("7");
    private JButton eightButton = new JButton("8");
    private JButton nineButton = new JButton("9");
    private String number = "";
    private JLabel numDisplay = new JLabel("0");
    private boolean addition = false;
    private boolean subtraction = false;
    private String numOverall;
    private CalculationModel theModel;


    private int total = 0;
    private boolean isEquals = false;   // false = haven't clicked equals button yet, true they have
    //private String numberText;

    Calc(){
        JPanel calcPanel = new JPanel();

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(400, 600);
        calcPanel.add(numDisplay);
        calcPanel.add(plusButton);
        calcPanel.add(minusButton);
        calcPanel.add(clearButton);
        calcPanel.add(equalsButton);
        calcPanel.add(zeroButton);
        calcPanel.add(oneButton);
        calcPanel.add(twoButton);
        calcPanel.add(threeButton);
        calcPanel.add(fourButton);
        calcPanel.add(fiveButton);
        calcPanel.add(sixButton);
        calcPanel.add(sevenButton);
        calcPanel.add(eightButton);
        calcPanel.add(nineButton);

        this.add(calcPanel);

        plusButton.addActionListener(this);
        minusButton.addActionListener(this);
        clearButton.addActionListener(this);
        equalsButton.addActionListener(this);
        zeroButton.addActionListener(this);
        oneButton.addActionListener(this);
        twoButton.addActionListener(this);
        threeButton.addActionListener(this);
        fourButton.addActionListener(this);
        fiveButton.addActionListener(this);
        sixButton.addActionListener(this);
        sevenButton.addActionListener(this);
        eightButton.addActionListener(this);
        nineButton.addActionListener(this);




    }
    public void actionPerformed(ActionEvent event){
        if (event.getSource() instanceof JButton){
            JButton clickedButton = (JButton) event.getSource();
            String buttonText = clickedButton.getText();
            numOverall = (Integer.toString(theModel.getCalculationValue()));
            if (clickedButton == zeroButton || clickedButton == oneButton || clickedButton == twoButton || clickedButton == threeButton || clickedButton == fourButton || clickedButton == fiveButton || clickedButton == sixButton || clickedButton == sevenButton || clickedButton == eightButton || clickedButton == nineButton)
            {
                number = number + buttonText;
                numDisplay.setText(number);
            }
            if (clickedButton == clearButton){
                number = "";
                addition = false;
                subtraction = false;
                total = 0;
                numDisplay.setText(number);
            }
            if (clickedButton == plusButton){
                addition = true;
                subtraction = false;
                total = Integer.parseInt(number);

            }
            if (clickedButton == minusButton){
                addition = false;
                subtraction = true;
                total = Integer.parseInt(number);

                //number = "";
            }
            if (clickedButton == equalsButton){
                isEquals = true;
                addition = false;
                subtraction = false;
                numDisplay.setText(numOverall);

            }
        }
    }

    public int getNumber(){
        return Integer.parseInt(number);
    }
    public boolean addition(){
        return addition;
    }
    public boolean subtraction(){
        return subtraction;
    }
    public int total(){
        return total;
    }
    public boolean isEquals(){
        return isEquals;
    }
    public String getNumberString(){
        return number;
    }
}

模型类

public class CalculationModel {
    private int calculationValue;

    public void addTwoNumbers(int firstNumber, int secondNumber){

        calculationValue = firstNumber + secondNumber;

    }
    public void minusTwoNumbers(int firstNumber, int secondNumber){
        calculationValue = firstNumber - secondNumber;
    }

    public int getCalculationValue(){

        return calculationValue;

    }
}

mvc 计算类

public class MVCCalc {
    public static void main(String[] args) {

        Calc theView = new Calc();

        CalculationModel theModel = new CalculationModel();

        Calculations theController = new Calculations(theView,theModel);

        theView.setVisible(true);

    }


}

计算类

public class Calculations
{
    private Calc theView;
    private CalculationModel theModel;

    private int number1;
    private int total;


    public Calculations(Calc theView, CalculationModel theModel)
    {
        this.theView = theView;
        this.theModel = theModel;
    }

    public void doesometing() {
        if(theView.isEquals()){
            if(theView.addition()){     


            number1 = theView.getNumber();
            total = theView.total();
            theModel.addTwoNumbers(total, number1);
            }
            if(theView.subtraction())
            {
            number1 = theView.getNumber();
            total = theView.total();
            theModel.minusTwoNumbers(total, number1);
        }
            //theView.addition() = false;
            //theView.subtraction() = false;
    }   
    }
}

最佳答案

Calc 类中的 Model 始终为 null。

关于java - 使用 MVC java 计算器出现一堆错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15713752/

相关文章:

java - 如何使用循环向 JTable 添加项目

java - 在运行时禁用 JComboBox 项

windows - Delphi 中的 Sqrt 函数

javascript - 如何使用 Javascript 从输入框值中获取总和?

java - android webview加载图像

java - 如何在java中将具有叶值和整个层次结构的CSV转换为JSON?

java - Mapreduce链接作业因异常而失败

java - 使用 JTabbedPane 自动扩展

floating-point - 计算器如何精确工作?

Java 文件 : Suppressing hardcoded text warnings