用eclipse开发一个计算器程序
时间: 2024-05-12 14:14:55 浏览: 92
eclipse开发计算器app
好的,以下是实现一个简单计算器程序的步骤:
1. 创建一个 Java 项目并打开 Eclipse。
2. 在项目中创建一个名为 "Calculator" 的包。
3. 在该包中创建一个名为 "Calculator" 的 Java 类。
4. 在 Calculator 类中添加 main 方法。
5. 创建一个 JFrame 对象并将其设置为可见。
6. 添加一个 JPanel 对象到 JFrame 中。
7. 在 JPanel 中添加一个 JTextField 对象用于显示计算结果。
8. 在 JPanel 中添加多个 JButton 对象用于输入数字和操作符。
9. 给每个 JButton 添加 ActionListener。
10. 在 ActionListener 中编写相应的计算逻辑,并将结果显示在 JTextField 中。
以下是示例代码:
```java
package Calculator;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
private JFrame frame;
private JPanel panel;
private JTextField textField;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonPlus, buttonMinus, buttonMultiply, buttonDivide, buttonEquals, buttonClear;
private String operator;
private double firstNumber, secondNumber, result;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator window = new Calculator();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Calculator() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Calculator");
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
frame.getContentPane().add(panel);
textField = new JTextField();
panel.add(textField);
button7 = new JButton("7");
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "7");
}
});
panel.add(button7);
button8 = new JButton("8");
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "8");
}
});
panel.add(button8);
button9 = new JButton("9");
button9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "9");
}
});
panel.add(button9);
buttonPlus = new JButton("+");
buttonPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstNumber = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
}
});
panel.add(buttonPlus);
button4 = new JButton("4");
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "4");
}
});
panel.add(button4);
button5 = new JButton("5");
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "5");
}
});
panel.add(button5);
button6 = new JButton("6");
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "6");
}
});
panel.add(button6);
buttonMinus = new JButton("-");
buttonMinus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstNumber = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
}
});
panel.add(buttonMinus);
button1 = new JButton("1");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "1");
}
});
panel.add(button1);
button2 = new JButton("2");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "2");
}
});
panel.add(button2);
button3 = new JButton("3");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "3");
}
});
panel.add(button3);
buttonMultiply = new JButton("*");
buttonMultiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstNumber = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
}
});
panel.add(buttonMultiply);
buttonClear = new JButton("C");
buttonClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
}
});
panel.add(buttonClear);
button0 = new JButton("0");
button0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "0");
}
});
panel.add(button0);
buttonEquals = new JButton("=");
buttonEquals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
secondNumber = Double.parseDouble(textField.getText());
if (operator.equals("+")) {
result = firstNumber + secondNumber;
} else if (operator.equals("-")) {
result = firstNumber - secondNumber;
} else if (operator.equals("*")) {
result = firstNumber * secondNumber;
} else if (operator.equals("/")) {
result = firstNumber / secondNumber;
}
textField.setText(Double.toString(result));
}
});
panel.add(buttonEquals);
buttonDivide = new JButton("/");
buttonDivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstNumber = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
}
});
panel.add(buttonDivide);
}
}
```
这个示例程序中包括了加、减、乘、除四种基本运算,以及清除和等于两种操作。你可以根据需要添加其他操作符和功能。
阅读全文