[英]How to get number displayed on MVC calculator java?
我正試圖讓它像普通計算器一樣在計算器上顯示數字的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 + buttonText;
numDisplay.setText(number);
我認為您缺少的是在actionPerformed方法的末尾調用numDisplay.setText(number)。 無論何時要更改文本,都應調用此方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.