利用 Java 开发一个可以进行简单的四则运算的图形化计算器,要求计算器至少能够实现基本的加减乘除运算、开方和清空功能,其中C按钮用于实现清空操作,直接将显示置零。
时间: 2024-03-08 19:47:50 浏览: 118
一个计算器的设计具有基本的加、减、乘、除等四则运算
3星 · 编辑精心推荐
好的,这是一个比较简单的任务,您需要以下几个步骤来完成这个任务:
1. 创建一个 Java GUI 窗口,在窗口中添加按钮和文本框等组件。
2. 为每个按钮添加监听器,当用户点击按钮时,执行相应的操作。
3. 实现四则运算和开方功能的算法,可以使用 Java 中的 Math 类库来实现。
4. 在文本框中显示计算结果。
以下是代码示例,仅供参考:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private JButton buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonSqrt, buttonClear, buttonEqual;
private double num1, num2, result;
private String operator;
public Calculator() {
// 设置窗口标题和大小
super("Calculator");
setSize(300, 300);
// 创建文本框和按钮
textField = new JTextField(20);
button0 = new JButton("0");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
buttonAdd = new JButton("+");
buttonSubtract = new JButton("-");
buttonMultiply = new JButton("*");
buttonDivide = new JButton("/");
buttonSqrt = new JButton("sqrt");
buttonClear = new JButton("C");
buttonEqual = new JButton("=");
// 添加按钮监听器
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);
buttonAdd.addActionListener(this);
buttonSubtract.addActionListener(this);
buttonMultiply.addActionListener(this);
buttonDivide.addActionListener(this);
buttonSqrt.addActionListener(this);
buttonClear.addActionListener(this);
buttonEqual.addActionListener(this);
// 添加组件到窗口
JPanel panel1 = new JPanel(new GridLayout(4, 4));
panel1.add(button7);
panel1.add(button8);
panel1.add(button9);
panel1.add(buttonAdd);
panel1.add(button4);
panel1.add(button5);
panel1.add(button6);
panel1.add(buttonSubtract);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(buttonMultiply);
panel1.add(buttonSqrt);
panel1.add(button0);
panel1.add(buttonClear);
panel1.add(buttonDivide);
JPanel panel2 = new JPanel(new FlowLayout());
panel2.add(textField);
JPanel panel3 = new JPanel(new FlowLayout());
panel3.add(buttonEqual);
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(panel2, BorderLayout.NORTH);
container.add(panel1, BorderLayout.CENTER);
container.add(panel3, BorderLayout.SOUTH);
// 显示窗口
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() == buttonAdd) {
num1 = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
} else if (e.getSource() == buttonSubtract) {
num1 = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
} else if (e.getSource() == buttonMultiply) {
num1 = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
} else if (e.getSource() == buttonDivide) {
num1 = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
} else if (e.getSource() == buttonSqrt) {
num1 = Double.parseDouble(textField.getText());
result = Math.sqrt(num1);
textField.setText(Double.toString(result));
} else if (e.getSource() == buttonClear) {
textField.setText("");
} else if (e.getSource() == buttonEqual) {
num2 = Double.parseDouble(textField.getText());
if (operator.equals("+")) {
result = num1 + num2;
} else if (operator.equals("-")) {
result = num1 - num2;
} else if (operator.equals("*")) {
result = num1 * num2;
} else if (operator.equals("/")) {
result = num1 / num2;
}
textField.setText(Double.toString(result));
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
以上代码实现了一个简单的图形化计算器,支持四则运算和开方功能,使用了 Java 的 GUI 编程和事件监听。
阅读全文