java可视化类实现计算器代码
时间: 2023-12-08 18:05:13 浏览: 44
下面是一个基于Java Swing实现的简单计算器代码:
```java
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class Calculator extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField display;
private double result;
private String operator;
private boolean start;
private JPanel panel;
public Calculator() {
display = new JTextField("0", 12);
display.setHorizontalAlignment(JTextField.RIGHT);
display.setEditable(false);
display.setBorder(new LineBorder(Color.BLACK));
result = 0;
operator = "=";
start = true;
panel = new JPanel(new GridLayout(4, 4));
String[] buttons = {
"7", "8", "9", "+",
"4", "5", "6", "-",
"1", "2", "3", "*",
"0", ".", "+/-", "/"
};
for (String button : buttons) {
createButton(button);
}
add(display, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void createButton(String label) {
JButton button = new JButton(label);
button.addActionListener(e -> {
String input = button.getText();
if (input.equals(".")) {
if (display.getText().indexOf(".") == -1) {
display.setText(display.getText() + ".");
}
} else if (input.equals("+/-")) {
double value = Double.parseDouble(display.getText());
if (value != 0) {
value = -value;
display.setText(Double.toString(value));
}
} else if (input.equals("C")) {
display.setText("0");
result = 0;
operator = "=";
start = true;
} else {
if (start) {
if (input.equals("-")) {
display.setText(input);
start = false;
} else {
operator = input;
}
} else {
double x = Double.parseDouble(display.getText());
calculate(x);
operator = input;
start = true;
}
}
});
panel.add(button);
}
private void calculate(double x) {
switch (operator) {
case "+":
result += x;
break;
case "-":
result -= x;
break;
case "*":
result *= x;
break;
case "/":
result /= x;
break;
case "=":
result = x;
break;
default:
break;
}
display.setText(Double.toString(result));
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
这个计算器实现了简单的加减乘除运算、小数点输入、正负号切换、清除操作等功能。通过Java Swing提供的GUI组件,实现了一个漂亮的用户界面。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)