用Java编写一个程序实现如下界面并实现基本计算机功能
时间: 2024-03-08 08:49:53 浏览: 33
为了实现这个界面和基本计算器功能,你可以按照以下步骤进行操作:
1. 创建一个 Java 项目。
2. 在项目中创建一个名为 "Calculator" 的类。
3. 在 "Calculator" 类中创建一个 JFrame 对象,用于显示计算器界面。
4. 在 JFrame 对象中添加显示计算结果的文本框和数字按钮。
5. 为数字按钮添加 Action 监听器,用于将数字添加到文本框中。
6. 添加操作符按钮,如 "+"、"-"、"*"、"/" 和 "="。
7. 为操作符按钮添加 Action 监听器,用于实现基本计算器功能。
8. 实现基本计算器功能。
以下是一个可能的实现示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator {
private JFrame frame;
private JTextField textField;
private double firstNum, secondNum, result;
private String operator;
public Calculator() {
frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLayout(new GridLayout(4, 4));
textField = new JTextField();
textField.setEditable(false);
frame.add(textField);
// 添加数字按钮
for (int i = 0; i <= 9; i++) {
JButton button = new JButton(String.valueOf(i));
button.addActionListener(new DigitActionListener());
frame.add(button);
}
// 添加操作符按钮
JButton addButton = new JButton("+");
addButton.addActionListener(new OperatorActionListener());
frame.add(addButton);
JButton subtractButton = new JButton("-");
subtractButton.addActionListener(new OperatorActionListener());
frame.add(subtractButton);
JButton multiplyButton = new JButton("*");
multiplyButton.addActionListener(new OperatorActionListener());
frame.add(multiplyButton);
JButton divideButton = new JButton("/");
divideButton.addActionListener(new OperatorActionListener());
frame.add(divideButton);
JButton equalButton = new JButton("=");
equalButton.addActionListener(new OperatorActionListener());
frame.add(equalButton);
frame.setVisible(true);
}
private class DigitActionListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String digit = event.getActionCommand();
textField.setText(textField.getText() + digit);
}
}
private class OperatorActionListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (operator != null) {
secondNum = Double.parseDouble(textField.getText());
switch (operator) {
case "+":
result = firstNum + secondNum;
break;
case "-":
result = firstNum - secondNum;
break;
case "*":
result = firstNum * secondNum;
break;
case "/":
result = firstNum / secondNum;
break;
}
textField.setText(String.valueOf(result));
} else {
firstNum = Double.parseDouble(textField.getText());
}
operator = event.getActionCommand();
textField.setText("");
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
```
这是一个简单的计算器实现。你可以按照自己的需求进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)