利用java实现计算器
时间: 2023-08-13 22:03:15 浏览: 96
用Java实现计算器
好的,实现计算器一般可以分为以下几个步骤:
1. 设计UI界面,包括数字键、操作符键、文本框等组件。
2. 实现数字键和操作符键的响应事件,通过按钮的监听器来实现。
3. 实现计算逻辑,根据用户输入的数字和操作符进行计算,并将结果显示在文本框中。
下面是一个简单的示例代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton buttonAdd, buttonSub, buttonMul, buttonDiv, buttonEqual;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private double num1, num2;
private String operator, input;
public Calculator() {
setTitle("Calculator");
setLayout(new BorderLayout());
textField = new JTextField();
textField.setEditable(false);
add(textField, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
button1 = new JButton("1");
button1.addActionListener(this);
buttonPanel.add(button1);
button2 = new JButton("2");
button2.addActionListener(this);
buttonPanel.add(button2);
button3 = new JButton("3");
button3.addActionListener(this);
buttonPanel.add(button3);
buttonAdd = new JButton("+");
buttonAdd.addActionListener(this);
buttonPanel.add(buttonAdd);
button4 = new JButton("4");
button4.addActionListener(this);
buttonPanel.add(button4);
button5 = new JButton("5");
button5.addActionListener(this);
buttonPanel.add(button5);
button6 = new JButton("6");
button6.addActionListener(this);
buttonPanel.add(button6);
buttonSub = new JButton("-");
buttonSub.addActionListener(this);
buttonPanel.add(buttonSub);
button7 = new JButton("7");
button7.addActionListener(this);
buttonPanel.add(button7);
button8 = new JButton("8");
button8.addActionListener(this);
buttonPanel.add(button8);
button9 = new JButton("9");
button9.addActionListener(this);
buttonPanel.add(button9);
buttonMul = new JButton("*");
buttonMul.addActionListener(this);
buttonPanel.add(buttonMul);
button0 = new JButton("0");
button0.addActionListener(this);
buttonPanel.add(button0);
buttonEqual = new JButton("=");
buttonEqual.addActionListener(this);
buttonPanel.add(buttonEqual);
buttonDiv = new JButton("/");
buttonDiv.addActionListener(this);
buttonPanel.add(buttonDiv);
add(buttonPanel, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button0) {
input += "0";
textField.setText(input);
} else if (e.getSource() == button1) {
input += "1";
textField.setText(input);
} else if (e.getSource() == button2) {
input += "2";
textField.setText(input);
} else if (e.getSource() == button3) {
input += "3";
textField.setText(input);
} else if (e.getSource() == button4) {
input += "4";
textField.setText(input);
} else if (e.getSource() == button5) {
input += "5";
textField.setText(input);
} else if (e.getSource() == button6) {
input += "6";
textField.setText(input);
} else if (e.getSource() == button7) {
input += "7";
textField.setText(input);
} else if (e.getSource() == button8) {
input += "8";
textField.setText(input);
} else if (e.getSource() == button9) {
input += "9";
textField.setText(input);
} else if (e.getSource() == buttonAdd) {
num1 = Double.parseDouble(input);
operator = "+";
input = "";
} else if (e.getSource() == buttonSub) {
num1 = Double.parseDouble(input);
operator = "-";
input = "";
} else if (e.getSource() == buttonMul) {
num1 = Double.parseDouble(input);
operator = "*";
input = "";
} else if (e.getSource() == buttonDiv) {
num1 = Double.parseDouble(input);
operator = "/";
input = "";
} else if (e.getSource() == buttonEqual) {
num2 = Double.parseDouble(input);
double result = 0;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
}
textField.setText("" + result);
input = "";
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这是一个简单的计算器,您可以根据自己的需求进行修改和完善。
阅读全文