用java编写一个简易计算器,要求拥有图形界面,可以显示运行过程即结果,可以进行简单的加减乘除以及符合运算
时间: 2023-06-25 20:02:19 浏览: 156
以下是一个简易计算器的Java代码,使用Swing库作为图形界面:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator {
private JFrame frame;
private JTextField displayField;
private String currentInput = "";
private double result = 0;
private String operator = "";
public Calculator() {
frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
JPanel panel = new JPanel(new GridLayout(4, 4));
JButton button1 = new JButton("1");
button1.addActionListener(new NumberListener());
panel.add(button1);
JButton button2 = new JButton("2");
button2.addActionListener(new NumberListener());
panel.add(button2);
JButton button3 = new JButton("3");
button3.addActionListener(new NumberListener());
panel.add(button3);
JButton buttonPlus = new JButton("+");
buttonPlus.addActionListener(new OperatorListener());
panel.add(buttonPlus);
JButton button4 = new JButton("4");
button4.addActionListener(new NumberListener());
panel.add(button4);
JButton button5 = new JButton("5");
button5.addActionListener(new NumberListener());
panel.add(button5);
JButton button6 = new JButton("6");
button6.addActionListener(new NumberListener());
panel.add(button6);
JButton buttonMinus = new JButton("-");
buttonMinus.addActionListener(new OperatorListener());
panel.add(buttonMinus);
JButton button7 = new JButton("7");
button7.addActionListener(new NumberListener());
panel.add(button7);
JButton button8 = new JButton("8");
button8.addActionListener(new NumberListener());
panel.add(button8);
JButton button9 = new JButton("9");
button9.addActionListener(new NumberListener());
panel.add(button9);
JButton buttonMultiply = new JButton("*");
buttonMultiply.addActionListener(new OperatorListener());
panel.add(buttonMultiply);
JButton buttonClear = new JButton("C");
buttonClear.addActionListener(new ClearListener());
panel.add(buttonClear);
JButton button0 = new JButton("0");
button0.addActionListener(new NumberListener());
panel.add(button0);
JButton buttonEquals = new JButton("=");
buttonEquals.addActionListener(new EqualsListener());
panel.add(buttonEquals);
JButton buttonDivide = new JButton("/");
buttonDivide.addActionListener(new OperatorListener());
panel.add(buttonDivide);
displayField = new JTextField();
displayField.setEditable(false);
frame.getContentPane().add(BorderLayout.NORTH, displayField);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.setVisible(true);
}
class NumberListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String digit = event.getActionCommand();
currentInput += digit;
displayField.setText(currentInput);
}
}
class OperatorListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String op = event.getActionCommand();
double input = Double.parseDouble(currentInput);
currentInput = "";
if (operator.equals("+")) {
result += input;
} else if (operator.equals("-")) {
result -= input;
} else if (operator.equals("*")) {
result *= input;
} else if (operator.equals("/")) {
result /= input;
} else {
result = input;
}
operator = op;
displayField.setText(Double.toString(result));
}
}
class EqualsListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
double input = Double.parseDouble(currentInput);
currentInput = "";
if (operator.equals("+")) {
result += input;
} else if (operator.equals("-")) {
result -= input;
} else if (operator.equals("*")) {
result *= input;
} else if (operator.equals("/")) {
result /= input;
} else {
result = input;
}
operator = "";
displayField.setText(Double.toString(result));
}
}
class ClearListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
currentInput = "";
result = 0;
operator = "";
displayField.setText("");
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
```
该计算器可以进行简单的加减乘除运算,并且拥有图形界面,可以显示运算结果。用户可以通过点击数字按钮来输入数字,点击加减乘除按钮来选择运算符,点击等于按钮来计算结果。同时,用户也可以通过点击清空按钮来清空输入内容和计算结果。
阅读全文