eclipse做计算器源代码下载
时间: 2023-07-18 16:38:19 浏览: 52
以下是一个简单的计算器程序的 Java 源代码,可以在 Eclipse 中进行编译和运行:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private JButton buttonAdd, buttonSub, buttonMul, buttonDiv, buttonClear, buttonEquals;
private JTextField textField;
private double num1 = 0;
private double num2 = 0;
private char operator;
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setResizable(false);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
textField = new JTextField();
textField.setEditable(false);
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
buttonDiv = new JButton("/");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
buttonMul = new JButton("*");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
buttonSub = new JButton("-");
button0 = new JButton("0");
buttonClear = new JButton("C");
buttonEquals = new JButton("=");
buttonAdd = new JButton("+");
panel.add(button7);
panel.add(button8);
panel.add(button9);
panel.add(buttonDiv);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(buttonMul);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(buttonSub);
panel.add(button0);
panel.add(buttonClear);
panel.add(buttonEquals);
panel.add(buttonAdd);
button0.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
buttonClear.addActionListener(this);
buttonAdd.addActionListener(this);
buttonSub.addActionListener(this);
buttonMul.addActionListener(this);
buttonDiv.addActionListener(this);
buttonEquals.addActionListener(this);
setLayout(new BorderLayout());
add(textField, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button0) {
textField.setText(textField.getText() + "0");
} else if (e.getSource() == button1) {
textField.setText(textField.getText() + "1");
} else if (e.getSource() == button2) {
textField.setText(textField.getText() + "2");
} else if (e.getSource() == button3) {
textField.setText(textField.getText() + "3");
} else if (e.getSource() == button4) {
textField.setText(textField.getText() + "4");
} else if (e.getSource() == button5) {
textField.setText(textField.getText() + "5");
} else if (e.getSource() == button6) {
textField.setText(textField.getText() + "6");
} else if (e.getSource() == button7) {
textField.setText(textField.getText() + "7");
} else if (e.getSource() == button8) {
textField.setText(textField.getText() + "8");
} else if (e.getSource() == button9) {
textField.setText(textField.getText() + "9");
} else if (e.getSource() == buttonClear) {
num1 = 0;
num2 = 0;
operator = ' ';
textField.setText("");
} else if (e.getSource() == buttonAdd) {
num1 = Double.parseDouble(textField.getText());
operator = '+';
textField.setText("");
} else if (e.getSource() == buttonSub) {
num1 = Double.parseDouble(textField.getText());
operator = '-';
textField.setText("");
} else if (e.getSource() == buttonMul) {
num1 = Double.parseDouble(textField.getText());
operator = '*';
textField.setText("");
} else if (e.getSource() == buttonDiv) {
num1 = Double.parseDouble(textField.getText());
operator = '/';
textField.setText("");
} else if (e.getSource() == buttonEquals) {
num2 = Double.parseDouble(textField.getText());
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(Double.toString(result));
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
在 Eclipse 中创建一个新的 Java 项目,将上述代码复制并粘贴到一个名为 `Calculator.java` 的新类中。然后,使用 Eclipse 中的菜单选项进行编译和运行即可。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)