用java编写一个简易的计算器,要求能进行加减乘除运算,通过鼠标点击按键交互
时间: 2023-06-24 10:04:59 浏览: 129
以下是一个简单的 Java 计算器实现,使用 Swing 组件库构建 UI 界面,实现加减乘除四则运算。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
private JButton btnPlus, btnMinus, btnMultiply, btnDivide, btnEqual, btnClear;
private double num1 = 0, num2 = 0, result = 0;
private String operator = "";
public Calculator() {
setTitle("Calculator");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Setup UI
JPanel panel = new JPanel(new GridLayout(4, 4, 10, 10));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
textField = new JTextField();
textField.setEditable(false);
panel.add(textField);
btn0 = new JButton("0");
btn1 = new JButton("1");
btn2 = new JButton("2");
btn3 = new JButton("3");
btn4 = new JButton("4");
btn5 = new JButton("5");
btn6 = new JButton("6");
btn7 = new JButton("7");
btn8 = new JButton("8");
btn9 = new JButton("9");
btnPlus = new JButton("+");
btnMinus = new JButton("-");
btnMultiply = new JButton("*");
btnDivide = new JButton("/");
btnEqual = new JButton("=");
btnClear = new JButton("C");
panel.add(btn7);
panel.add(btn8);
panel.add(btn9);
panel.add(btnPlus);
panel.add(btn4);
panel.add(btn5);
panel.add(btn6);
panel.add(btnMinus);
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
panel.add(btnMultiply);
panel.add(btn0);
panel.add(btnClear);
panel.add(btnEqual);
panel.add(btnDivide);
add(panel);
// Add event listeners
btn0.addActionListener(this);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btnPlus.addActionListener(this);
btnMinus.addActionListener(this);
btnMultiply.addActionListener(this);
btnDivide.addActionListener(this);
btnEqual.addActionListener(this);
btnClear.addActionListener(this);
}
// Handle button click events
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn0) {
textField.setText(textField.getText() + "0");
} else if (e.getSource() == btn1) {
textField.setText(textField.getText() + "1");
} else if (e.getSource() == btn2) {
textField.setText(textField.getText() + "2");
} else if (e.getSource() == btn3) {
textField.setText(textField.getText() + "3");
} else if (e.getSource() == btn4) {
textField.setText(textField.getText() + "4");
} else if (e.getSource() == btn5) {
textField.setText(textField.getText() + "5");
} else if (e.getSource() == btn6) {
textField.setText(textField.getText() + "6");
} else if (e.getSource() == btn7) {
textField.setText(textField.getText() + "7");
} else if (e.getSource() == btn8) {
textField.setText(textField.getText() + "8");
} else if (e.getSource() == btn9) {
textField.setText(textField.getText() + "9");
} else if (e.getSource() == btnPlus) {
num1 = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
} else if (e.getSource() == btnMinus) {
num1 = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
} else if (e.getSource() == btnMultiply) {
num1 = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
} else if (e.getSource() == btnDivide) {
num1 = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
} else if (e.getSource() == btnEqual) {
num2 = Double.parseDouble(textField.getText());
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
}
textField.setText(Double.toString(result));
} else if (e.getSource() == btnClear) {
textField.setText("");
}
}
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.setVisible(true);
}
}
```
该计算器实现了一个 `JTextField` 用于显示输入和计算结果,以及多个 `JButton` 用于表示数字、运算符和清除按钮。其中,数字按钮和运算符按钮都绑定了 `ActionListener` 监听器,通过判断事件源的不同来实现不同的操作。计算器的核心计算逻辑在 `actionPerformed` 方法中实现,根据不同的运算符执行不同的计算操作,并将结果显示在文本框中。
注意,在计算过程中需要将字符串转换成浮点数进行计算,最后再将结果转换回字符串显示在文本框中。
完整代码:https://github.com/JackieLin0328/calculator-java
阅读全文