MVC スタイルを使用して Java 電卓を作成しています。最初に入力した数字と 2 番目に入力した数字を区別するのに苦労しています。プラスまたはマイナスを入力すると true になるブール値を入力すると、数値文字列が空白文字列 "" に戻ります。しかし、このブール値が真のときに取得しようとすると、文字列はすでに空白になっているため、手遅れです。最初の番号を取得してから 2 番目の番号を取得する方法について何かアドバイスはありますか? この電卓はプラスとマイナスを使っているだけです。イコールクリアボタン付き。ありがとう
計算クラス
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Calc extends JFrame implements ActionListener {
private JTextField numDisplay = new JTextField(10);
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 boolean trueFalse; //plus or minus
private boolean onOff = false; //false = 1st int, true = 2nd int
private int total;
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 = "";
onOff = false;
}
if (clickedButton == plusButton){
trueFalse = true;
onOff = true;
}
if (clickedButton == minusButton){
trueFalse = false;
onOff = true;
number = "";
}
if (clickedButton == equalsButton){
isEquals = true;
}
}
}
public int getNumber(){
return Integer.parseInt(number);
}
public boolean trueFalse(){
return trueFalse;
}
public boolean onOff(){
return onOff;
}
public int total(){
return total;
}
public boolean isEquals(){
return isEquals;
}
public String getNumberString(){
return number;
}
}
計算クラス
public class Calculations
{
private Calc theView;
private CalculationModel theModel;
private boolean onOff = theView.onOff();
private boolean trueFalse = theView.trueFalse();
private int number1;
public Calculations(Calc theView, CalculationModel theModel)
{
this.theView = theView;
this.theModel = theModel;
}
public void doesometing() {
if(trueFalse)
{
number1 = theView.getNumber();
}
}
}