用Java的gui简易计算器
时间: 2023-10-04 18:12:21 浏览: 94
基于Java实现的(GUI)简易计算器【100012745】
5星 · 资源好评率100%
好的,以下是一个简单的Java GUI计算器的示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton[] numberButtons = new JButton[10];
private JButton[] functionButtons = new JButton[8];
private JButton addButton, subButton, mulButton, divButton;
private JButton decButton, equButton, delButton, clrButton;
private JPanel panel;
private Font font = new Font("Verdana", Font.BOLD, 18);
private double num1 = 0, num2 = 0, result = 0;
private char operator;
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(330, 450);
setResizable(false);
textField = new JTextField();
textField.setBounds(0, 0, 320, 50);
textField.setFont(font);
textField.setEditable(false);
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
numberButtons[i].setFont(font);
}
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
decButton = new JButton(".");
equButton = new JButton("=");
delButton = new JButton("Del");
clrButton = new JButton("Clr");
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(font);
}
panel = new JPanel();
panel.setBounds(0, 50, 320, 400);
panel.setLayout(new GridLayout(5, 4, 10, 10));
panel.add(delButton);
panel.add(clrButton);
panel.add(functionButtons[0]);
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(functionButtons[1]);
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(functionButtons[2]);
panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(functionButtons[3]);
panel.add(decButton);
panel.add(numberButtons[0]);
panel.add(equButton);
add(textField);
add(panel);
setVisible(true);
}
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 str = textField.getText();
textField.setText("");
for (int i = 0; i < str.length() - 1; i++) {
textField.setText(textField.getText() + str.charAt(i));
}
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
该计算器具有基本的加减乘除和小数点功能,以及清除和删除按钮。您可以根据需要进行修改和扩展。
阅读全文