用java实现一个windows系统自带计算器
时间: 2023-06-24 12:07:15 浏览: 131
下面是用 Java Swing 实现的一个简单的 Windows 计算器:
```java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
// 显示运算结果的文本框
private JTextField resultText;
// 数字按钮
private JButton[] numberButtons = new JButton[10];
// 运算符按钮
private JButton addButton, subButton, mulButton, divButton, equalButton, dotButton;
// 存储数字和运算符的字符串
private String num1 = "", num2 = "", operator = "";
public Calculator() {
initUI();
}
private void initUI() {
setTitle("计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
// 显示结果的文本框
resultText = new JTextField(12);
resultText.setEditable(false);
resultText.setHorizontalAlignment(JTextField.RIGHT);
// 数字按钮
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
}
// 运算符按钮
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
equalButton = new JButton("=");
dotButton = new JButton(".");
addButton.addActionListener(this);
subButton.addActionListener(this);
mulButton.addActionListener(this);
divButton.addActionListener(this);
equalButton.addActionListener(this);
dotButton.addActionListener(this);
// 将数字按钮放在面板上
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 3));
for (int i = 1; i < 10; i++) {
buttonPanel.add(numberButtons[i]);
}
buttonPanel.add(new JPanel());
buttonPanel.add(numberButtons[0]);
buttonPanel.add(dotButton);
// 将运算符按钮放在面板上
JPanel operatorPanel = new JPanel();
operatorPanel.setLayout(new GridLayout(2, 2));
operatorPanel.add(addButton);
operatorPanel.add(subButton);
operatorPanel.add(mulButton);
operatorPanel.add(divButton);
// 将等号按钮放在面板上
JPanel equalPanel = new JPanel();
equalPanel.setLayout(new FlowLayout());
equalPanel.add(equalButton);
// 将文本框和按钮面板放在顶层面板上
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
topPanel.add(resultText, BorderLayout.NORTH);
topPanel.add(buttonPanel, BorderLayout.CENTER);
// 将运算符面板和等号面板放在底层面板上
JPanel botPanel = new JPanel();
botPanel.setLayout(new BorderLayout());
botPanel.add(operatorPanel, BorderLayout.CENTER);
botPanel.add(equalPanel, BorderLayout.EAST);
// 将顶层面板和底层面板放在窗口上
setLayout(new BorderLayout());
add(topPanel, BorderLayout.NORTH);
add(botPanel, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
if (operator.isEmpty()) {
num1 += command;
resultText.setText(num1);
} else {
num2 += command;
resultText.setText(num2);
}
break;
case "+":
case "-":
case "*":
case "/":
operator = command;
break;
case ".":
if (operator.isEmpty()) {
if (!num1.contains(".")) {
num1 += ".";
resultText.setText(num1);
}
} else {
if (!num2.contains(".")) {
num2 += ".";
resultText.setText(num2);
}
}
break;
case "=":
if (!num1.isEmpty() && !num2.isEmpty() && !operator.isEmpty()) {
double n1 = Double.parseDouble(num1);
double n2 = Double.parseDouble(num2);
double result = 0;
switch (operator) {
case "+":
result = n1 + n2;
break;
case "-":
result = n1 - n2;
break;
case "*":
result = n1 * n2;
break;
case "/":
if (n2 == 0) {
resultText.setText("除数不能为0");
return;
}
result = n1 / n2;
break;
}
num1 = String.valueOf(result);
num2 = "";
operator = "";
resultText.setText(num1);
}
break;
}
}
}
```
运行程序后,会弹出一个窗口,里面有数字按钮、运算符按钮和一个文本框,用户可以点击按钮输入数字和选择运算符来进行计算。
阅读全文