Java图形用户界面设计,编写计算器代码
时间: 2023-06-18 22:06:33 浏览: 112
下面是一个简单的Java图形用户界面计算器代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CalculatorGUI implements ActionListener {
JFrame frame;
JTextField textField;
JButton[] numberButtons = new JButton[10];
JButton[] functionButtons = new JButton[8];
JButton addButton, subButton, mulButton, divButton;
JButton decButton, equButton, delButton, clrButton;
JPanel panel;
double num1 = 0, num2 = 0, result = 0;
char operator;
CalculatorGUI() {
frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(325, 450);
frame.setLayout(null);
textField = new JTextField();
textField.setBounds(50, 25, 200, 50);
textField.setFont(new Font("Arial", Font.PLAIN, 25));
textField.setEditable(false);
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
decButton = new JButton(".");
equButton = new JButton("=");
delButton = new JButton("Delete");
clrButton = new JButton("Clear");
functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;
functionButtons[4] = decButton;
functionButtons[5] = equButton;
functionButtons[6] = delButton;
functionButtons[7] = clrButton;
for (int i = 0; i < 8; i++) {
functionButtons[i].addActionListener(this);
functionButtons[i].setFont(new Font("Arial", Font.PLAIN, 18));
}
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
numberButtons[i].setFont(new Font("Arial", Font.PLAIN, 18));
}
delButton.setBounds(50, 300, 150, 50);
clrButton.setBounds(200, 300, 150, 50);
panel = new JPanel();
panel.setBounds(50, 100, 300, 200);
panel.setLayout(new GridLayout(4, 4, 10, 10));
panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(addButton);
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(subButton);
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(mulButton);
panel.add(decButton);
panel.add(numberButtons[0]);
panel.add(equButton);
panel.add(divButton);
frame.add(panel);
frame.add(delButton);
frame.add(clrButton);
frame.add(textField);
frame.setVisible(true);
}
public static void main(String[] args) {
CalculatorGUI calculator = new CalculatorGUI();
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons[i]) {
textField.setText(textField.getText() + String.valueOf(i));
}
}
if (e.getSource() == decButton) {
textField.setText(textField.getText() + ".");
}
if (e.getSource() == addButton) {
num1 = Double.parseDouble(textField.getText());
operator = '+';
textField.setText("");
}
if (e.getSource() == subButton) {
num1 = Double.parseDouble(textField.getText());
operator = '-';
textField.setText("");
}
if (e.getSource() == mulButton) {
num1 = Double.parseDouble(textField.getText());
operator = '*';
textField.setText("");
}
if (e.getSource() == divButton) {
num1 = Double.parseDouble(textField.getText());
operator = '/';
textField.setText("");
}
if (e.getSource() == equButton) {
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;
default:
result = 0;
}
textField.setText(String.valueOf(result));
num1 = result;
}
if (e.getSource() == clrButton) {
textField.setText("");
}
if (e.getSource() == delButton) {
String str = textField.getText();
textField.setText("");
for (int i = 0; i < str.length() - 1; i++) {
textField.setText(textField.getText() + str.charAt(i));
}
}
}
}
```
该代码创建了一个基本的计算器,使用Java的Swing库创建图形用户界面。它包含数字按钮、运算符按钮和功能按钮,例如“删除”和“清除”。当用户单击数字按钮时,数字将显示在文本框中。当用户单击运算符按钮时,第一个数字将保存,并清空文本框以等待下一个数字。当用户单击等于按钮时,程序将计算两个数字之间的运算结果,并将结果显示在文本框中。
阅读全文