java - 如何获取MVC计算器java上显示的数字?

标签 java swing jlabel calculator

我试图让它像普通计算器一样在计算器上显示数字的 JLabel。但我不知道该怎么做,我所能做的就是让它成为一个不会改变的标签。我将数字字符串放在那里,但在单击数字后它并没有像应有的那样改变。有什么建议吗?

/* 
 * 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(number);
    private boolean addition = false;
    private boolean subtraction = false;


    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();
            if (clickedButton == zeroButton || clickedButton == oneButton || clickedButton == twoButton || clickedButton == threeButton || clickedButton == fourButton || clickedButton == fiveButton || clickedButton == sixButton || clickedButton == sevenButton || clickedButton == eightButton || clickedButton == nineButton)
            {
                number = number + buttonText;
            }
            if (clickedButton == clearButton){
                number = "";
                addition = false;
                subtraction = false;
                total = 0;
            }
            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;
            }
        }
    }

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

最佳答案

private JLabel numDisplay = new JLabel(number);

这行代码所做的就是初始化 numDisplay 变量以包含变量 number 的文本。这并不意味着每次更改数字变量时标签都会自动更新。我没有仔细查看您的代码,但您可以尝试:

number = number + buttonText;
numDisplay.setText(number);

关于java - 如何获取MVC计算器java上显示的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15712580/

相关文章:

java - java线程间通信

java - Tomcat 8 和 Jersey 2.7 返回 404 错误

java - 将 WebSphere Portal 应用程序转换为标准 Java EE 应用程序

java - 将组件粘贴到 JScrollPane 上

java - 将鼠标监听器添加到动态添加到面板的 JLabel

java - 如何在android中发出具有多个属性的ksoap2请求?

java - JLabel 属性更改时位于顶层

java - 如何在JTree java swing中显示BinaryTree

java - 无法在具有背景图像的 JLabel 上显示文本

java - 设置 JLabel 的宽度并在悬停时添加工具提示