简易计算器。比如:按键得到23*2.2,单击“=”得出结果50.6。“MC”键实现清除右侧的显示内容。界面如下图所示。
时间: 2024-03-18 20:39:08 浏览: 107
很抱歉,我之前的回答有些错误。以下是一个简单的Java Eclipse计算器的源代码,仅供参考:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame {
private static final long serialVersionUID = 1L;
//创建文本框
JTextField text = new JTextField("0");
//定义计算器需要的变量
double number1 = 0.0;
double number2 = 0.0;
double result = 0.0;
String operator = "";
boolean isFirstDigit = true;
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
public Calculator() {
super();
//设置窗口属性
setTitle("简易计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(250, 300);
setLocation(500, 200);
//设置面板布局
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
//将文本框添加到面板中
panel.add(text, BorderLayout.NORTH);
//创建按钮
JButton btn0 = new JButton("0");
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
JButton btnAdd = new JButton("+");
JButton btnSub = new JButton("-");
JButton btnMul = new JButton("*");
JButton btnDiv = new JButton("/");
JButton btnEq = new JButton("=");
JButton btnClear = new JButton("MC");
//创建一个监听器
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (isFirstDigit) {
text.setText("");
isFirstDigit = false;
}
text.setText(text.getText() + command);
}
};
//给数字按钮添加监听器
btn0.addActionListener(listener);
btn1.addActionListener(listener);
btn2.addActionListener(listener);
btn3.addActionListener(listener);
btn4.addActionListener(listener);
btn5.addActionListener(listener);
btn6.addActionListener(listener);
btn7.addActionListener(listener);
btn8.addActionListener(listener);
btn9.addActionListener(listener);
//给操作符按钮添加监听器
ActionListener operatorListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
operator = e.getActionCommand();
number1 = Double.parseDouble(text.getText());
isFirstDigit = true;
}
};
btnAdd.addActionListener(operatorListener);
btnSub.addActionListener(operatorListener);
btnMul.addActionListener(operatorListener);
btnDiv.addActionListener(operatorListener);
//给“=”按钮添加监听器
btnEq.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
number2 = Double.parseDouble(text.getText());
switch (operator) {
case "+":
result = number1 + number2;
break;
case "-":
result = number1 - number2;
break;
case "*":
result = number1 * number2;
break;
case "/":
result = number1 / number2;
break;
}
text.setText(String.valueOf(result));
isFirstDigit = true;
}
});
//给“MC”按钮添加监听器
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setText("0");
isFirstDigit = true;
}
});
//创建面板,将按钮添加到面板中
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
buttonPanel.add(btn7);
buttonPanel.add(btn8);
buttonPanel.add(btn9);
buttonPanel.add(btnDiv);
buttonPanel.add(btn4);
buttonPanel.add(btn5);
buttonPanel.add(btn6);
buttonPanel.add(btnMul);
buttonPanel.add(btn1);
buttonPanel.add(btn2);
buttonPanel.add(btn3);
buttonPanel.add(btnSub);
buttonPanel.add(btn0);
buttonPanel.add(btnClear);
buttonPanel.add(btnEq);
buttonPanel.add(btnAdd);
//将面板添加到主面板中
panel.add(buttonPanel, BorderLayout.CENTER);
//将主面板添加到窗口中
this.setContentPane(panel);
}
}
```
希望这个源代码对你有所帮助。
阅读全文