swing模拟简易计算器
时间: 2023-09-30 19:08:25 浏览: 48
以下是一个基于 Swing 的简易计算器的示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private JButton[] buttons;
private String[] buttonLabels = {
"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"
};
private double result = 0.0;
private String operator = "";
private boolean isOperatorClicked = false;
public Calculator() {
super("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
display = new JTextField("0", 20);
display.setHorizontalAlignment(JTextField.RIGHT);
display.setEditable(false);
buttons = new JButton[buttonLabels.length];
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
panel.add(buttons[i]);
}
add(display, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String input = e.getActionCommand();
if (input.equals("C")) {
result = 0.0;
operator = "";
isOperatorClicked = false;
display.setText("0");
} else if (input.equals(".")) {
if (display.getText().indexOf(".") == -1) {
display.setText(display.getText() + ".");
}
} else if (input.equals("+") || input.equals("-") || input.equals("*") || input.equals("/")) {
if (isOperatorClicked) {
operator = input;
} else {
isOperatorClicked = true;
String currentText = display.getText();
if (!currentText.equals("")) {
result = Double.parseDouble(currentText);
operator = input;
display.setText("");
}
}
} else if (input.equals("=")) {
String currentText = display.getText();
if (!currentText.equals("")) {
double d = Double.parseDouble(currentText);
if (operator.equals("+")) {
result += d;
} else if (operator.equals("-")) {
result -= d;
} else if (operator.equals("*")) {
result *= d;
} else if (operator.equals("/")) {
result /= d;
}
display.setText(Double.toString(result));
isOperatorClicked = false;
}
} else {
if (isOperatorClicked) {
display.setText(input);
isOperatorClicked = false;
} else {
display.setText(display.getText() + input);
}
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
```
该示例代码创建了一个基于 Swing 的简易计算器,支持加、减、乘、除和小数点等操作。可以通过运行 `main` 方法启动该计算器。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)