利用java编写一个计算机窗口,计算机可以计算加减乘除,pow、sqrt、log10
时间: 2024-02-01 17:04:52 浏览: 59
如何使用java来实现加减乘除运算.docx
好的,以下是一个简单的 Java 计算器窗口,可以进行加减乘除、pow、sqrt、log10 的计算:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private double num1, num2, result;
private int operator;
private boolean isFirstNum = true;
public Calculator() {
super("Calculator");
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
// 文本框
textField = new JTextField(10);
textField.setEditable(false);
add(textField, BorderLayout.NORTH);
// 操作按钮
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
JButton button7 = new JButton("7");
button7.addActionListener(this);
panel.add(button7);
JButton button8 = new JButton("8");
button8.addActionListener(this);
panel.add(button8);
JButton button9 = new JButton("9");
button9.addActionListener(this);
panel.add(button9);
JButton buttonAdd = new JButton("+");
buttonAdd.addActionListener(this);
panel.add(buttonAdd);
JButton button4 = new JButton("4");
button4.addActionListener(this);
panel.add(button4);
JButton button5 = new JButton("5");
button5.addActionListener(this);
panel.add(button5);
JButton button6 = new JButton("6");
button6.addActionListener(this);
panel.add(button6);
JButton buttonSubtract = new JButton("-");
buttonSubtract.addActionListener(this);
panel.add(buttonSubtract);
JButton button1 = new JButton("1");
button1.addActionListener(this);
panel.add(button1);
JButton button2 = new JButton("2");
button2.addActionListener(this);
panel.add(button2);
JButton button3 = new JButton("3");
button3.addActionListener(this);
panel.add(button3);
JButton buttonMultiply = new JButton("*");
buttonMultiply.addActionListener(this);
panel.add(buttonMultiply);
JButton button0 = new JButton("0");
button0.addActionListener(this);
panel.add(button0);
JButton buttonDot = new JButton(".");
buttonDot.addActionListener(this);
panel.add(buttonDot);
JButton buttonEquals = new JButton("=");
buttonEquals.addActionListener(this);
panel.add(buttonEquals);
JButton buttonDivide = new JButton("/");
buttonDivide.addActionListener(this);
panel.add(buttonDivide);
JButton buttonPow = new JButton("pow");
buttonPow.addActionListener(this);
panel.add(buttonPow);
JButton buttonSqrt = new JButton("sqrt");
buttonSqrt.addActionListener(this);
panel.add(buttonSqrt);
JButton buttonLog10 = new JButton("log10");
buttonLog10.addActionListener(this);
panel.add(buttonLog10);
add(panel, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("+")) {
operator = 1;
num1 = Double.parseDouble(textField.getText());
isFirstNum = false;
textField.setText("");
} else if (command.equals("-")) {
operator = 2;
num1 = Double.parseDouble(textField.getText());
isFirstNum = false;
textField.setText("");
} else if (command.equals("*")) {
operator = 3;
num1 = Double.parseDouble(textField.getText());
isFirstNum = false;
textField.setText("");
} else if (command.equals("/")) {
operator = 4;
num1 = Double.parseDouble(textField.getText());
isFirstNum = false;
textField.setText("");
} else if (command.equals("pow")) {
operator = 5;
num1 = Double.parseDouble(textField.getText());
isFirstNum = false;
textField.setText("");
} else if (command.equals("sqrt")) {
operator = 6;
num1 = Double.parseDouble(textField.getText());
result = Math.sqrt(num1);
textField.setText(Double.toString(result));
} else if (command.equals("log10")) {
operator = 7;
num1 = Double.parseDouble(textField.getText());
result = Math.log10(num1);
textField.setText(Double.toString(result));
} else if (command.equals("=")) {
if (isFirstNum) {
return;
}
num2 = Double.parseDouble(textField.getText());
switch (operator) {
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
result = num1 / num2;
break;
case 5:
result = Math.pow(num1, num2);
break;
}
textField.setText(Double.toString(result));
isFirstNum = true;
} else {
textField.setText(textField.getText() + command);
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个计算器窗口包含一个文本框和一些按钮。当你点击数字按钮时,数字将显示在文本框中。当你点击操作符按钮时,计算器将保存第一个数字并清空文本框,等待输入第二个数字。当你点击等于号时,计算器将执行相应的操作并将结果显示在文本框中。
阅读全文