用java设计计算器窗口
时间: 2024-03-23 07:36:25 浏览: 81
好的,我可以为您提供一些基本的Java代码,用于设计一个简单的计算器窗口。
首先,您需要使用Java GUI库来创建窗口。这里我将使用Swing库,它是Java标准库的一部分。
以下是一个示例代码,可以帮助您创建一个具有基本操作的计算器窗口:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button0;
private JButton addButton, subtractButton, multiplyButton, divideButton, equalButton, clearButton;
private double num1 = 0, num2 = 0, result = 0;
private int operator = 0;
public Calculator() {
setTitle("Calculator");
setSize(300, 300);
setLayout(new GridLayout(5, 3));
setDefaultCloseOperation(EXIT_ON_CLOSE);
textField = new JTextField();
add(textField);
button1 = new JButton("1");
add(button1);
button1.addActionListener(this);
button2 = new JButton("2");
add(button2);
button2.addActionListener(this);
button3 = new JButton("3");
add(button3);
button3.addActionListener(this);
button4 = new JButton("4");
add(button4);
button4.addActionListener(this);
button5 = new JButton("5");
add(button5);
button5.addActionListener(this);
button6 = new JButton("6");
add(button6);
button6.addActionListener(this);
button7 = new JButton("7");
add(button7);
button7.addActionListener(this);
button8 = new JButton("8");
add(button8);
button8.addActionListener(this);
button9 = new JButton("9");
add(button9);
button9.addActionListener(this);
clearButton = new JButton("C");
add(clearButton);
clearButton.addActionListener(this);
button0 = new JButton("0");
add(button0);
button0.addActionListener(this);
addButton = new JButton("+");
add(addButton);
addButton.addActionListener(this);
subtractButton = new JButton("-");
add(subtractButton);
subtractButton.addActionListener(this);
multiplyButton = new JButton("*");
add(multiplyButton);
multiplyButton.addActionListener(this);
divideButton = new JButton("/");
add(divideButton);
divideButton.addActionListener(this);
equalButton = new JButton("=");
add(equalButton);
equalButton.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
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() == button0) {
textField.setText(textField.getText() + "0");
} else if (e.getSource() == clearButton) {
textField.setText("");
num1 = 0;
num2 = 0;
result = 0;
operator = 0;
} else if (e.getSource() == addButton) {
num1 = Double.parseDouble(textField.getText());
operator = 1;
textField.setText("");
} else if (e.getSource() == subtractButton) {
num1 = Double.parseDouble(textField.getText());
operator = 2;
textField.setText("");
} else if (e.getSource() == multiplyButton) {
num1 = Double.parseDouble(textField.getText());
operator = 3;
textField.setText("");
} else if (e.getSource() == divideButton) {
num1 = Double.parseDouble(textField.getText());
operator = 4;
textField.setText("");
} else if (e.getSource() == equalButton) {
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;
}
textField.setText(Double.toString(result));
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
```
以上代码创建了一个带有数字按钮和操作符按钮的计算器窗口。在单击这些按钮时,它们将追加到文本字段中。还有一个“C”按钮,用于清除文本字段和操作数。当单击加、减、乘、除或等于按钮时,它们将执行相应的数学运算,并将结果显示在文本字段中。
希望这可以帮助您开始设计您自己的计算器窗口!
阅读全文