我遇到的问题是我不知道如何制作 +、-、/ 和 * 按钮来保存我之前输入的数字,然后在我按下 = 按钮时使用该数字。我也无法让 = 按钮识别我按下的四个按钮中的哪个按钮,以便当我按下 - 按钮时它会分轨,而不是相乘。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener{
JPanel row1 = new JPanel();
JTextField display = new JTextField("0");
Font font = new Font("Calibri", Font.PLAIN, 24);
JPanel row2 = new JPanel();
JButton c = new JButton("C");
JPanel row3 = new JPanel();
JButton num7 = new JButton("7");
JButton num8 = new JButton("8");
JButton num9 = new JButton("9");
JButton divide = new JButton("/");
JPanel row4 = new JPanel();
JButton num4 = new JButton("4");
JButton num5 = new JButton("5");
JButton num6 = new JButton("6");
JButton multiply = new JButton("*");
JPanel row5 = new JPanel();
JButton num1 = new JButton("1");
JButton num2 = new JButton("2");
JButton num3 = new JButton("3");
JButton minus = new JButton("-");
JPanel row6 = new JPanel();
JButton num0 = new JButton("0");
JButton dot = new JButton(".");
JButton equals = new JButton("=");
JButton plus = new JButton("+");
public Calculator(){
super("Calculator");
setSize(450,275);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(6,1);
setLayout(layout);
display.setFont(font);
display.setEnabled(false);
GridLayout layout1 = new GridLayout(1,1);
row1.setLayout(layout1);
row1.add(display);
add(row1);
c.addActionListener(this);
c.setFont(font);
row2.setLayout(layout1);
row2.add(c);
add(row2);
num7.setFont(font);
num8.setFont(font);
num9.setFont(font);
divide.setFont(font);
num7.addActionListener(this);
num8.addActionListener(this);
num9.addActionListener(this);
divide.addActionListener(this);
GridLayout layout2 = new GridLayout(1,4);
row3.setLayout(layout2);
row3.add(num7);
row3.add(num8);
row3.add(num9);
row3.add(divide);
add(row3);
num4.setFont(font);
num5.setFont(font);
num6.setFont(font);
multiply.setFont(font);
num4.addActionListener(this);
num5.addActionListener(this);
num6.addActionListener(this);
multiply.addActionListener(this);
row4.setLayout(layout2);
row4.add(num4);
row4.add(num5);
row4.add(num6);
row4.add(multiply);
add(row4);
num1.setFont(font);
num2.setFont(font);
num3.setFont(font);
minus.setFont(font);
num1.addActionListener(this);
num2.addActionListener(this);
num3.addActionListener(this);
minus.addActionListener(this);
row5.setLayout(layout2);
row5.add(num1);
row5.add(num2);
row5.add(num3);
row5.add(minus);
add(row5);
num0.setFont(font);
dot.setFont(font);
equals.setFont(font);
plus.setFont(font);
num0.addActionListener(this);
dot.addActionListener(this);
equals.addActionListener(this);
plus.addActionListener(this);
row6.setLayout(layout2);
row6.add(num0);
row6.add(dot);
row6.add(equals);
row6.add(plus);
add(row6);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String button = e.getActionCommand();
if(button.equals("C")){
display.setText("0");
}
if(button.equals("7")){
int num = 7;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}
if(button.equals("8")){
int num = 8;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}
if(button.equals("9")){
int num = 9;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}
if(button.equals("4")){
int num = 4;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}
if(button.equals("5")){
int num = 5;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}
if(button.equals("6")){
int num = 6;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}
if(button.equals("1")){
int num = 1;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}
if(button.equals("2")){
int num = 2;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}
if(button.equals("3")){
int num = 3;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}
if(button.equals("0")){
int num = 0;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}
}
public static void main(String[] args){
Calculator frame = new Calculator();
}
}