java代码编写具有图形界面的计算机
时间: 2023-06-14 13:02:49 浏览: 58
以下是一个简单的Java代码示例,用于创建一个具有图形用户界面(GUI)的计算器:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CalculatorGUI extends JFrame implements ActionListener {
private JTextField textField;
private JButton[] buttons;
private String[] buttonLabels = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};
private double num1, num2, result;
private char operator;
public CalculatorGUI() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
// Create the text field
textField = new JTextField(20);
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
add(textField, BorderLayout.NORTH);
// Create the buttons
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
buttons = new JButton[buttonLabels.length];
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
add(buttonPanel, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) {
String buttonLabel = e.getActionCommand();
if (buttonLabel.matches("[0-9.]")) {
textField.setText(textField.getText() + buttonLabel);
} else if (buttonLabel.matches("[/+\\-*]")) {
num1 = Double.parseDouble(textField.getText());
operator = buttonLabel.charAt(0);
textField.setText("");
} else if (buttonLabel.equals("=")) {
num2 = Double.parseDouble(textField.getText());
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
textField.setText(String.valueOf(result));
} else if (buttonLabel.equals("C")) {
textField.setText("");
}
}
public static void main(String[] args) {
CalculatorGUI calculator = new CalculatorGUI();
calculator.setVisible(true);
}
}
```
该代码使用了Java Swing库来创建GUI。它包含一个文本字段和一个按钮面板,该面板包含数字、运算符和其他控件。在用户按下按钮时,actionPerformed()方法将被调用,并根据按钮标签执行相应的操作。在该示例中,它执行了基本的四则运算,并在文本字段中显示结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)