javaswing计算器
时间: 2023-10-08 17:05:45 浏览: 131
好的,下面是一个简单的 Java Swing 计算器示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
// 创建界面组件
private JTextField textField;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private JButton buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonEqual, buttonClear;
// 记录计算结果和当前操作数
private double result = 0;
private double currentNumber = 0;
private String operator = "";
public Calculator() {
// 设置窗口标题和大小
setTitle("Calculator");
setSize(300, 350);
setResizable(false);
// 创建界面组件
textField = new JTextField();
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
button0 = new JButton("0");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
buttonAdd = new JButton("+");
buttonSubtract = new JButton("-");
buttonMultiply = new JButton("*");
buttonDivide = new JButton("/");
buttonEqual = new JButton("=");
buttonClear = new JButton("C");
// 设置组件位置和大小
textField.setBounds(10, 20, 270, 50);
button7.setBounds(10, 80, 50, 50);
button8.setBounds(70, 80, 50, 50);
button9.setBounds(130, 80, 50, 50);
buttonDivide.setBounds(190, 80, 50, 50);
button4.setBounds(10, 140, 50, 50);
button5.setBounds(70, 140, 50, 50);
button6.setBounds(130, 140, 50, 50);
buttonMultiply.setBounds(190, 140, 50, 50);
button1.setBounds(10, 200, 50, 50);
button2.setBounds(70, 200, 50, 50);
button3.setBounds(130, 200, 50, 50);
buttonSubtract.setBounds(190, 200, 50, 50);
button0.setBounds(10, 260, 50, 50);
buttonClear.setBounds(70, 260, 50, 50);
buttonEqual.setBounds(130, 260, 50, 50);
buttonAdd.setBounds(190, 260, 50, 50);
// 添加组件到界面
add(textField);
add(button7);
add(button8);
add(button9);
add(buttonDivide);
add(button4);
add(button5);
add(button6);
add(buttonMultiply);
add(button1);
add(button2);
add(button3);
add(buttonSubtract);
add(button0);
add(buttonClear);
add(buttonEqual);
add(buttonAdd);
// 添加事件监听器
button0.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
buttonAdd.addActionListener(this);
buttonSubtract.addActionListener(this);
buttonMultiply.addActionListener(this);
buttonDivide.addActionListener(this);
buttonEqual.addActionListener(this);
buttonClear.addActionListener(this);
// 设置窗口可见性
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("C")) {
// 清空计算器
result = 0;
currentNumber = 0;
operator = "";
textField.setText("");
} else if (command.equals("+")) {
// 加法
result += currentNumber;
currentNumber = 0;
operator = "+";
textField.setText("");
} else if (command.equals("-")) {
// 减法
result -= currentNumber;
currentNumber = 0;
operator = "-";
textField.setText("");
} else if (command.equals("*")) {
// 乘法
result *= currentNumber;
currentNumber = 0;
operator = "*";
textField.setText("");
} else if (command.equals("/")) {
// 除法
if (currentNumber == 0) {
textField.setText("Error");
} else {
result /= currentNumber;
currentNumber = 0;
operator = "/";
textField.setText("");
}
} else if (command.equals("=")) {
// 等于号
if (operator.equals("+")) {
result += currentNumber;
} else if (operator.equals("-")) {
result -= currentNumber;
} else if (operator.equals("*")) {
result *= currentNumber;
} else if (operator.equals("/")) {
if (currentNumber == 0) {
textField.setText("Error");
} else {
result /= currentNumber;
}
}
currentNumber = result;
operator = "";
textField.setText(Double.toString(result));
} else {
// 数字键
currentNumber = currentNumber * 10 + Double.parseDouble(command);
textField.setText(Double.toString(currentNumber));
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个计算器支持加、减、乘、除和清空操作,并且可以处理连续操作,例如 `1 + 2 - 3 * 4 / 5`。你可以根据需要进行修改和扩展。
阅读全文