Java实现简单功能的计算器界面,编写动作事件监听器并注册,实现简单运算
时间: 2024-02-13 11:04:24 浏览: 84
好的,以下是一个简单的 Java 程序,实现了一个计算器界面,支持加、减、乘、除四种运算:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton buttonAdd, buttonSubtract, buttonMultiply, buttonDivide;
private int num1, num2;
public Calculator() {
// 创建窗体和布局
super("简单计算器");
setLayout(new FlowLayout());
// 创建文本框和按钮
textField = new JTextField(20);
buttonAdd = new JButton("+");
buttonSubtract = new JButton("-");
buttonMultiply = new JButton("*");
buttonDivide = new JButton("/");
// 将按钮添加到窗体,并设置监听器
add(buttonAdd);
add(buttonSubtract);
add(buttonMultiply);
add(buttonDivide);
buttonAdd.addActionListener(this);
buttonSubtract.addActionListener(this);
buttonMultiply.addActionListener(this);
buttonDivide.addActionListener(this);
// 将文本框添加到窗体
add(textField);
// 设置窗体大小并显示
setSize(300, 100);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event) {
// 获取文本框中的数字
String text = textField.getText();
if (text.equals("")) {
num1 = 0;
} else {
num1 = Integer.parseInt(text);
}
// 获取按钮上的运算符
JButton button = (JButton) event.getSource();
String operator = button.getText();
// 清空文本框
textField.setText("");
// 注册第二个数字的动作事件监听器
buttonAdd.removeActionListener(this);
buttonSubtract.removeActionListener(this);
buttonMultiply.removeActionListener(this);
buttonDivide.removeActionListener(this);
buttonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
num2 = Integer.parseInt(textField.getText());
textField.setText("" + (num1 + num2));
buttonAdd.addActionListener(Calculator.this);
buttonSubtract.addActionListener(Calculator.this);
buttonMultiply.addActionListener(Calculator.this);
buttonDivide.addActionListener(Calculator.this);
}
});
buttonSubtract.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
num2 = Integer.parseInt(textField.getText());
textField.setText("" + (num1 - num2));
buttonAdd.addActionListener(Calculator.this);
buttonSubtract.addActionListener(Calculator.this);
buttonMultiply.addActionListener(Calculator.this);
buttonDivide.addActionListener(Calculator.this);
}
});
buttonMultiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
num2 = Integer.parseInt(textField.getText());
textField.setText("" + (num1 * num2));
buttonAdd.addActionListener(Calculator.this);
buttonSubtract.addActionListener(Calculator.this);
buttonMultiply.addActionListener(Calculator.this);
buttonDivide.addActionListener(Calculator.this);
}
});
buttonDivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
num2 = Integer.parseInt(textField.getText());
if (num2 == 0) {
textField.setText("除数不能为0");
} else {
textField.setText("" + (num1 / num2));
}
buttonAdd.addActionListener(Calculator.this);
buttonSubtract.addActionListener(Calculator.this);
buttonMultiply.addActionListener(Calculator.this);
buttonDivide.addActionListener(Calculator.this);
}
});
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个程序创建了一个窗体,包含一个文本框和四个按钮。点击加、减、乘、除四个按钮中的任意一个,程序会提示您输入两个数字,并显示计算结果。如果除数为0,则程序会提示“除数不能为0”。
阅读全文