用JAVA编写一个计算器,能实现加减乘除、开平方、求阶乘等计算功能,计算器有图形化界面
时间: 2024-06-09 18:11:34 浏览: 221
好的,我会尽力满足您的需求。为了实现您的要求,我们可以使用 Java Swing 进行图形化界面开发,下面是一个简单的 Java 计算器实现:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JPanel panel;
private JTextField textField;
private JButton addButton, subtractButton, multiplyButton, divideButton, sqrtButton, factorialButton, equalButton, clearButton;
private double num1 = 0;
private double num2 = 0;
private char operator;
public Calculator() {
setTitle("Calculator");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
panel = new JPanel(new GridLayout(4, 4));
textField = new JTextField();
addButton = new JButton("+");
subtractButton = new JButton("-");
multiplyButton = new JButton("*");
divideButton = new JButton("/");
sqrtButton = new JButton("sqrt");
factorialButton = new JButton("!");
equalButton = new JButton("=");
clearButton = new JButton("C");
addButton.addActionListener(this);
subtractButton.addActionListener(this);
multiplyButton.addActionListener(this);
divideButton.addActionListener(this);
sqrtButton.addActionListener(this);
factorialButton.addActionListener(this);
equalButton.addActionListener(this);
clearButton.addActionListener(this);
panel.add(addButton);
panel.add(subtractButton);
panel.add(multiplyButton);
panel.add(divideButton);
panel.add(sqrtButton);
panel.add(factorialButton);
panel.add(equalButton);
panel.add(clearButton);
panel.add(textField);
add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
operator = '+';
num1 = Double.parseDouble(textField.getText());
textField.setText("");
} else if (e.getSource() == subtractButton) {
operator = '-';
num1 = Double.parseDouble(textField.getText());
textField.setText("");
} else if (e.getSource() == multiplyButton) {
operator = '*';
num1 = Double.parseDouble(textField.getText());
textField.setText("");
} else if (e.getSource() == divideButton) {
operator = '/';
num1 = Double.parseDouble(textField.getText());
textField.setText("");
} else if (e.getSource() == sqrtButton) {
num1 = Double.parseDouble(textField.getText());
num1 = Math.sqrt(num1);
textField.setText(String.valueOf(num1));
} else if (e.getSource() == factorialButton) {
num1 = Double.parseDouble(textField.getText());
num2 = 1;
for (int i = 1; i <= num1; i++) {
num2 = num2 * i;
}
textField.setText(String.valueOf(num2));
} else if (e.getSource() == equalButton) {
num2 = Double.parseDouble(textField.getText());
switch (operator) {
case '+':
num1 = num1 + num2;
break;
case '-':
num1 = num1 - num2;
break;
case '*':
num1 = num1 * num2;
break;
case '/':
num1 = num1 / num2;
break;
}
textField.setText(String.valueOf(num1));
} else if (e.getSource() == clearButton) {
num1 = 0;
num2 = 0;
operator = ' ';
textField.setText("");
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个程序能够实现加减乘除、开平方、求阶乘等计算功能,并且具有图形化界面。您可以将代码复制到您的 Java IDE 中进行编译和运行,或者将代码保存为 `Calculator.java` 文件,然后在命令行中使用 `javac Calculator.java` 命令进行编译,在使用 `java Calculator` 命令运行程序。
阅读全文