import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame {
private JTextField displayField;
private double num1 = 0;
private double num2 = 0;
private char operator;
public Calculator() {
// 设置窗口标题
setTitle("简易计算器");
// 设置窗口大小
setSize(300, 400);
// 设置窗口关闭时的操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置布局为边界布局
setLayout(new BorderLayout());
// 创建显示结果的文本框
displayField = new JTextField();
displayField.setEditable(false);
add(displayField, BorderLayout.NORTH);
// 创建面板用于放置按钮
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
// 数字按钮0-9
for (int i = 0; i < 10; i++) {
JButton button = new JButton(String.valueOf(i));
button.addActionListener(new ButtonClickListener());
buttonPanel.add(button);
}
// 操作符按钮 + - * /
JButton addButton = new JButton("+");
addButton.addActionListener(new OperatorClickListener('+'));
buttonPanel.add(addButton);
JButton subtractButton = new JButton("-");
subtractButton.addActionListener(new OperatorClickListener('-'));
buttonPanel.add(subtractButton);
JButton multiplyButton = new JButton("*");
multiplyButton.addActionListener(new OperatorClickListener('*'));
buttonPanel.add(multiplyButton);
JButton divideButton = new JButton("/");
divideButton.addActionListener(new OperatorClickListener('/'));
buttonPanel.add(divideButton);
// 等于按钮
JButton equalButton = new JButton("=");
equalButton.addActionListener(new EqualClickListener());
buttonPanel.add(equalButton);
// 清除按钮
JButton clearButton = new JButton("C");
clearButton.addActionListener(new ClearClickListener());
buttonPanel.add(clearButton);
add(buttonPanel, BorderLayout.CENTER);
}
// 数字按钮点击监听器
private class ButtonClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
displayField.setText(displayField.getText() + button.getText());
}
}
// 操作符按钮点击监听器
private class OperatorClickListener implements ActionListener {
private char op;
public OperatorClickListener(char op) {
this.op = op;
}
@Override
public void actionPerformed(ActionEvent e) {
num1 = Double.parseDouble(displayField.getText());
operator = op;
displayField.setText("");
}
}
// 等于按钮点击监听器
private class EqualClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
num2 = Double.parseDouble(displayField.getText());
double result = 0;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2!= 0) {
result = num1 / num2;
} else {
JOptionPane.showMessageDialog(Calculator.this, "除数不能为0", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
break;
}
displayField.setText(String.valueOf(result));
}
}
// 清除按钮点击监听器
private class ClearClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
displayField.setText("");
num1 = 0;
num2 = 0;
operator = ' ';
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Calculator calculator = new Calculator();
calculator.setVisible(true);
});
}
}
这段代码的主要思路如下: