编写Java GUI程序,实现一个简单计算器。要求如下: (1)设计的界面如下图所示:窗体的标题栏显示“模拟计算器—江海大”,1个文本框用于显示输入字符和计算结果;20个按钮控件作为字符输入按键或者功能按键。程序的外观对64开平方根的运行结果 (2)计算功能:实现加、减、乘、除等双目运算,开平方、百分数等单目运算; (3)辅助功能:按钮“C”清空文本框;按钮“←”退格,删除文本框中最右边的一个字符。
时间: 2023-06-26 07:10:29 浏览: 223
以下是一个简单的 Java GUI 程序,实现了一个简易计算器的功能,包括加、减、乘、除、开平方、百分数、清空和退格功能。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0;
private JButton btnAdd, btnSub, btnMul, btnDiv, btnSqrt, btnPercent, btnClear, btnDelete, btnEqual;
private double num1 = 0, num2 = 0, result = 0;
private String operator = "";
public Calculator() {
// 设置窗体的标题和大小
setTitle("模拟计算器-江海大");
setSize(350, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建一个面板,用于放置文本框和按钮
JPanel panel = new JPanel();
panel.setLayout(null);
// 创建文本框
textField = new JTextField();
textField.setBounds(10, 10, 320, 50);
textField.setFont(new Font("Arial", Font.PLAIN, 24));
textField.setEditable(false);
panel.add(textField);
// 创建数字按钮
btn1 = new JButton("1");
btn1.setBounds(10, 80, 60, 60);
btn1.setFont(new Font("Arial", Font.PLAIN, 24));
btn1.addActionListener(this);
panel.add(btn1);
btn2 = new JButton("2");
btn2.setBounds(80, 80, 60, 60);
btn2.setFont(new Font("Arial", Font.PLAIN, 24));
btn2.addActionListener(this);
panel.add(btn2);
btn3 = new JButton("3");
btn3.setBounds(150, 80, 60, 60);
btn3.setFont(new Font("Arial", Font.PLAIN, 24));
btn3.addActionListener(this);
panel.add(btn3);
btn4 = new JButton("4");
btn4.setBounds(10, 150, 60, 60);
btn4.setFont(new Font("Arial", Font.PLAIN, 24));
btn4.addActionListener(this);
panel.add(btn4);
btn5 = new JButton("5");
btn5.setBounds(80, 150, 60, 60);
btn5.setFont(new Font("Arial", Font.PLAIN, 24));
btn5.addActionListener(this);
panel.add(btn5);
btn6 = new JButton("6");
btn6.setBounds(150, 150, 60, 60);
btn6.setFont(new Font("Arial", Font.PLAIN, 24));
btn6.addActionListener(this);
panel.add(btn6);
btn7 = new JButton("7");
btn7.setBounds(10, 220, 60, 60);
btn7.setFont(new Font("Arial", Font.PLAIN, 24));
btn7.addActionListener(this);
panel.add(btn7);
btn8 = new JButton("8");
btn8.setBounds(80, 220, 60, 60);
btn8.setFont(new Font("Arial", Font.PLAIN, 24));
btn8.addActionListener(this);
panel.add(btn8);
btn9 = new JButton("9");
btn9.setBounds(150, 220, 60, 60);
btn9.setFont(new Font("Arial", Font.PLAIN, 24));
btn9.addActionListener(this);
panel.add(btn9);
btn0 = new JButton("0");
btn0.setBounds(80, 290, 60, 60);
btn0.setFont(new Font("Arial", Font.PLAIN, 24));
btn0.addActionListener(this);
panel.add(btn0);
// 创建运算符按钮
btnAdd = new JButton("+");
btnAdd.setBounds(220, 80, 60, 60);
btnAdd.setFont(new Font("Arial", Font.PLAIN, 24));
btnAdd.addActionListener(this);
panel.add(btnAdd);
btnSub = new JButton("-");
btnSub.setBounds(290, 80, 60, 60);
btnSub.setFont(new Font("Arial", Font.PLAIN, 24));
btnSub.addActionListener(this);
panel.add(btnSub);
btnMul = new JButton("×");
btnMul.setBounds(220, 150, 60, 60);
btnMul.setFont(new Font("Arial", Font.PLAIN, 24));
btnMul.addActionListener(this);
panel.add(btnMul);
btnDiv = new JButton("÷");
btnDiv.setBounds(290, 150, 60, 60);
btnDiv.setFont(new Font("Arial", Font.PLAIN, 24));
btnDiv.addActionListener(this);
panel.add(btnDiv);
btnSqrt = new JButton("√");
btnSqrt.setBounds(220, 220, 60, 60);
btnSqrt.setFont(new Font("Arial", Font.PLAIN, 24));
btnSqrt.addActionListener(this);
panel.add(btnSqrt);
btnPercent = new JButton("%");
btnPercent.setBounds(290, 220, 60, 60);
btnPercent.setFont(new Font("Arial", Font.PLAIN, 24));
btnPercent.addActionListener(this);
panel.add(btnPercent);
btnClear = new JButton("C");
btnClear.setBounds(220, 290, 60, 60);
btnClear.setFont(new Font("Arial", Font.PLAIN, 24));
btnClear.addActionListener(this);
panel.add(btnClear);
btnDelete = new JButton("←");
btnDelete.setBounds(290, 290, 60, 60);
btnDelete.setFont(new Font("Arial", Font.PLAIN, 24));
btnDelete.addActionListener(this);
panel.add(btnDelete);
btnEqual = new JButton("=");
btnEqual.setBounds(220, 10, 130, 50);
btnEqual.setFont(new Font("Arial", Font.PLAIN, 24));
btnEqual.addActionListener(this);
panel.add(btnEqual);
// 将面板添加到窗体中
add(panel);
// 显示窗体
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
// 数字按键
textField.setText(textField.getText() + command);
break;
case "+":
case "-":
case "×":
case "÷":
// 双目运算符按键
num1 = Double.parseDouble(textField.getText());
operator = command;
textField.setText("");
break;
case "√":
case "%":
// 单目运算符按键
num1 = Double.parseDouble(textField.getText());
operator = command;
calculate();
break;
case "C":
// 清空按键
num1 = 0;
num2 = 0;
operator = "";
result = 0;
textField.setText("");
break;
case "←":
// 退格按键
String text = textField.getText();
if (text.length() > 0) {
textField.setText(text.substring(0, text.length() - 1));
}
break;
case "=":
// 等号按键
num2 = Double.parseDouble(textField.getText());
calculate();
break;
}
}
private void calculate() {
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "×":
result = num1 * num2;
break;
case "÷":
result = num1 / num2;
break;
case "√":
result = Math.sqrt(num1);
break;
case "%":
result = num1 / 100;
break;
}
textField.setText(String.valueOf(result));
}
public static void main(String[] args) {
new Calculator();
}
}
```
运行程序后,可以看到一个简易计算器的界面,如下图所示:
![calculator](https://cdn.luogu.com.cn/upload/image_hosting/edn3d8tt.png)
可以通过点击不同的按钮,输入数字和运算符,进行计算。
阅读全文