package 计算器;
import javax.swing.*;
import java.awt.*;
public class Calculator extends JFrame {
// 显示结果的文本框
private JTextField resultTextField;
// 计算器按钮
private JButton buttonOne;
private JButton buttonTwo;
private JButton buttonThree;
private JButton buttonAdd;
private JButton buttonFour;
private JButton buttonFive;
private JButton buttonSix;
private JButton buttonSubtract;
private JButton buttonSeven;
private JButton buttonEight;
private JButton buttonNine;
private JButton buttonMultiply;
private JButton buttonClear;
private JButton buttonZero;
private JButton buttonEqual;
private JButton buttonDivide;
// 计算器操作数和运算符
private double operand1;
private double operand2;
private char operator;
public Calculator() {
super("Calculator");
// 创建显示结果的文本框
resultTextField = new JTextField(20);
resultTextField.setEditable(false);
// 创建计算器按钮
buttonOne = new JButton("1");
buttonTwo = new JButton("2");
buttonThree = new JButton("3");
buttonAdd = new JButton("+");
buttonFour = new JButton("4");
buttonFive = new JButton("5");
buttonSix = new JButton("6");
buttonSubtract = new JButton("-");
buttonSeven = new JButton("7");
buttonEight = new JButton("8");
buttonNine = new JButton("9");
buttonMultiply = new JButton("*");
buttonClear = new JButton("C");
buttonZero = new JButton("0");
buttonEqual = new JButton("=");
buttonDivide = new JButton("/");
// 添加按钮事件
buttonOne.addActionListener(e -> addToResult('1'));
buttonTwo.addActionListener(e -> addToResult('2'));
buttonThree.addActionListener(e -> addToResult('3'));
buttonAdd.addActionListener(e -> setOperator('+'));
buttonFour.addActionListener(e -> addToResult('4'));
buttonFive.addActionListener(e -> addToResult('5'));
buttonSix.addActionListener(e -> addToResult('6'));
buttonSubtract.addActionListener(e -> setOperator('-'));
buttonSeven.addActionListener(e -> addToResult('7'));
buttonEight.addActionListener(e -> addToResult('8'));
buttonNine.addActionListener(e -> addToResult('9'));
buttonMultiply.addActionListener(e -> setOperator('*'));
buttonClear.addActionListener(e -> clearResult());
buttonZero.addActionListener(e -> addToResult('0'));
buttonEqual.addActionListener(e -> calculateResult());
buttonDivide.addActionListener(e -> setOperator('/'));
// 创建计算器面板,添加组件
JPanel calculatorPanel = new JPanel();
calculatorPanel.setLayout(new GridLayout(4, 4));
calculatorPanel.add(buttonOne);
calculatorPanel.add(buttonTwo);
calculatorPanel.add(buttonThree);
calculatorPanel.add(buttonAdd);
calculatorPanel.add(buttonFour);
calculatorPanel.add(buttonFive);
calculatorPanel.add(buttonSix);
calculatorPanel.add(buttonSubtract);
calculatorPanel.add(buttonSeven);
calculatorPanel.add(buttonEight);
calculatorPanel.add(buttonNine);
calculatorPanel.add(buttonMultiply);
calculatorPanel.add(buttonClear);
calculatorPanel.add(buttonZero);
calculatorPanel.add(buttonEqual);
calculatorPanel.add(buttonDivide);
// 添加组件到主窗口
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(resultTextField, BorderLayout.NORTH);
contentPane.add(calculatorPanel, BorderLayout.CENTER);
// 设置主窗口属性
setSize(300, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addToResult(char digit) {
String current = resultTextField.getText();
resultTextField.setText(current + digit);
}
private void setOperator(char operation) {
operand1 = Double.parseDouble(resultTextField.getText());
operator = operation;
clearResult();
}
private void clearResult() {
resultTextField.setText("");
}
private void calculateResult() {
operand2 = Double.parseDouble(resultTextField.getText());
double result = 0;
switch (operator) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
}
resultTextField.setText(Double.toString(result));
}
public static void main(String[] args) {
new Calculator();
}
}