用java设计一个计算器窗口
时间: 2023-11-16 21:08:01 浏览: 167
好的,以下是一个简单的计算器窗口的Java代码示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField displayField;
private JButton[] digitButtons;
private JButton addButton, subtractButton, multiplyButton, divideButton, equalsButton, clearButton;
private String displayString = "";
private double result = 0;
private String operator = "";
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
displayField = new JTextField();
displayField.setHorizontalAlignment(JTextField.RIGHT);
displayField.setEditable(false);
contentPane.add(displayField, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
digitButtons = new JButton[10];
for (int i = 0; i < digitButtons.length; i++) {
digitButtons[i] = new JButton("" + i);
digitButtons[i].addActionListener(this);
}
addButton = new JButton("+");
addButton.addActionListener(this);
subtractButton = new JButton("-");
subtractButton.addActionListener(this);
multiplyButton = new JButton("*");
multiplyButton.addActionListener(this);
divideButton = new JButton("/");
divideButton.addActionListener(this);
equalsButton = new JButton("=");
equalsButton.addActionListener(this);
clearButton = new JButton("C");
clearButton.addActionListener(this);
buttonPanel.add(digitButtons[7]);
buttonPanel.add(digitButtons[8]);
buttonPanel.add(digitButtons[9]);
buttonPanel.add(divideButton);
buttonPanel.add(digitButtons[4]);
buttonPanel.add(digitButtons[5]);
buttonPanel.add(digitButtons[6]);
buttonPanel.add(multiplyButton);
buttonPanel.add(digitButtons[1]);
buttonPanel.add(digitButtons[2]);
buttonPanel.add(digitButtons[3]);
buttonPanel.add(subtractButton);
buttonPanel.add(digitButtons[0]);
buttonPanel.add(clearButton);
buttonPanel.add(equalsButton);
buttonPanel.add(addButton);
contentPane.add(buttonPanel, BorderLayout.CENTER);
setContentPane(contentPane);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.charAt(0) >= '0' && command.charAt(0) <= '9' || command.equals(".")) {
displayString += command;
displayField.setText(displayString);
} else if (command.equals("+")) {
compute();
operator = "+";
} else if (command.equals("-")) {
compute();
operator = "-";
} else if (command.equals("*")) {
compute();
operator = "*";
} else if (command.equals("/")) {
compute();
operator = "/";
} else if (command.equals("=")) {
compute();
operator = "";
} else if (command.equals("C")) {
displayString = "";
result = 0;
operator = "";
displayField.setText("");
}
}
private void compute() {
if (operator.equals("+")) {
result += Double.parseDouble(displayString);
} else if (operator.equals("-")) {
result -= Double.parseDouble(displayString);
} else if (operator.equals("*")) {
result *= Double.parseDouble(displayString);
} else if (operator.equals("/")) {
result /= Double.parseDouble(displayString);
} else {
result = Double.parseDouble(displayString);
}
displayString = "";
displayField.setText("" + result);
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
```
这段代码创建了一个简单的计算器窗口,其中包含一个显示器文本框和一组数字和操作符按钮。用户可以通过按下这些按钮来输入数字和操作符,并通过按下“=”按钮来计算结果。计算器还包含一个“C”按钮,用于清除当前输入。
阅读全文