gui 设计的简单计算器 java_Java实现GUI计算器(完整版)
时间: 2023-12-02 20:03:00 浏览: 68
好的,以下是一个简单的 Java GUI 计算器实现。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator implements ActionListener {
JFrame frame;
JTextField textField;
JButton[] numberButtons = new JButton[10];
JButton[] functionButtons = new JButton[9];
JButton addButton, subButton, mulButton, divButton;
JButton decButton, equButton, delButton, clrButton;
JPanel panel;
Font myFont = new Font("Ink Free", Font.BOLD, 30);
double num1 = 0, num2 = 0, result = 0;
char operator;
Calculator() {
frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420, 550);
frame.setLayout(null);
textField = new JTextField();
textField.setBounds(50, 25, 300, 50);
textField.setFont(myFont);
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(myFont);
functionButtons[i].setFocusable(false);
}
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
numberButtons[i].setFont(myFont);
numberButtons[i].setFocusable(false);
}
delButton.setBounds(50, 430, 145, 50);
clrButton.setBounds(205, 430, 145, 50);
panel = new JPanel();
panel.setBounds(50, 100, 300, 300);
panel.setLayout(new GridLayout(4, 4, 10, 10));
panel.setBackground(Color.white);
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) {
Calculator calculator = new Calculator();
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons[i]) {
textField.setText(textField.getText().concat(String.valueOf(i)));
}
}
if (e.getSource() == decButton) {
textField.setText(textField.getText().concat("."));
}
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;
}
textField.setText(String.valueOf(result));
num1 = result;
}
if (e.getSource() == clrButton) {
textField.setText("");
}
if (e.getSource() == delButton) {
String string = textField.getText();
textField.setText("");
for (int i = 0; i < string.length() - 1; i++) {
textField.setText(textField.getText() + string.charAt(i));
}
}
}
}
```
这个计算器使用了 Java 的 Swing 库实现了 GUI 界面,通过监听按钮的点击事件来实现计算器的基本功能。其中,数字按钮和功能按钮都继承自 JButton 类,并且使用 addActionListener() 方法注册了监听器。监听器实现了 ActionListener 接口,并且实现了 actionPerformed() 方法,在方法中根据点击的按钮来进行相应的操作。
在界面设计方面,使用了 JPanel 和 GridLayout 来实现了一个 4 行 4 列的按钮区域,并且使用了 JTextField 来显示计算结果。同时,为了美观,使用了 Ink Free 字体。
阅读全文