eclipse编写计算器java
时间: 2023-07-09 15:38:41 浏览: 93
android eclipse 编写的简易计算器
5星 · 资源好评率100%
好的,下面是一个简单的计算器的 Java 代码,使用 Eclipse 编写:
```java
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Calculator extends JFrame {
private JPanel contentPane;
private JTextField textField;
private double num1, num2, result;
private String operator;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator frame = new Calculator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 250, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(10, 10, 214, 30);
contentPane.add(textField);
textField.setColumns(10);
JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "7");
}
});
btn7.setBounds(10, 50, 50, 50);
contentPane.add(btn7);
JButton btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "8");
}
});
btn8.setBounds(70, 50, 50, 50);
contentPane.add(btn8);
JButton btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "9");
}
});
btn9.setBounds(130, 50, 50, 50);
contentPane.add(btn9);
JButton btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "4");
}
});
btn4.setBounds(10, 110, 50, 50);
contentPane.add(btn4);
JButton btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "5");
}
});
btn5.setBounds(70, 110, 50, 50);
contentPane.add(btn5);
JButton btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "6");
}
});
btn6.setBounds(130, 110, 50, 50);
contentPane.add(btn6);
JButton btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "1");
}
});
btn1.setBounds(10, 170, 50, 50);
contentPane.add(btn1);
JButton btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "2");
}
});
btn2.setBounds(70, 170, 50, 50);
contentPane.add(btn2);
JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "3");
}
});
btn3.setBounds(130, 170, 50, 50);
contentPane.add(btn3);
JButton btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "0");
}
});
btn0.setBounds(70, 230, 50, 50);
contentPane.add(btn0);
JButton btnAdd = new JButton("+");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1 = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
}
});
btnAdd.setBounds(190, 50, 50, 50);
contentPane.add(btnAdd);
JButton btnSubtract = new JButton("-");
btnSubtract.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1 = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
}
});
btnSubtract.setBounds(190, 110, 50, 50);
contentPane.add(btnSubtract);
JButton btnMultiply = new JButton("*");
btnMultiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1 = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
}
});
btnMultiply.setBounds(190, 170, 50, 50);
contentPane.add(btnMultiply);
JButton btnDivide = new JButton("/");
btnDivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num1 = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
}
});
btnDivide.setBounds(190, 230, 50, 50);
contentPane.add(btnDivide);
JButton btnEquals = new JButton("=");
btnEquals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
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));
}
});
btnEquals.setBounds(130, 230, 50, 50);
contentPane.add(btnEquals);
}
}
```
这个计算器使用了 Java 的 Swing 库来构建 GUI 界面,支持加、减、乘、除四种运算。运行代码后会弹出一个计算器窗口,你可以在窗口中输入数字和运算符,然后按下等号获取结果。
阅读全文