一、实验目的
1、掌握 Java 图形界面程序设计的基本思想和步骤。
2、掌握图形界面程序设计中键盘鼠标事件处理的机制。
3、了解常用的监听器接口及其方法和作用。
4、掌握图形界面程序设计中事件处理的方法和步骤。
二、实验类型
设计型
三、实验内容
1、在上一个实验(实验三)的第 1 题中,我们设计了一个简易计算器的界面。但仅仅要求设计出其 界面,而没有要求进行事件处理。在本次实验中,请实现其事件处理。使得程序运行之后,当输入两个 小数作为运算数后,点击“相加”或者“相减”按钮,两数相加或相减的结果就会显示在“运算结果” 框中。 如果点击“全部清零”按钮,那么两个运算数输入框和运算结果框中就会完全被清空。 2、对于第 1 题,除了正常数据之外,用户还可能因为好奇或误操作导致这样几种情况: (1)1 个 或 2 个运算数均未输入;(2)输入的数据串中含有除小数点和数字之外的非法字符;(3)输入的数据串 中不含有除小数点和数字之外的非法字符,但小数点的个数超过 1 个或小数点的位置在数据的开头或结 尾处。 请改进你的程序,对这几种情况进行判断,不允许使用正则表达式或异常处理机制,完全使用字 符串类的各种方法,对这几种输入情况得到的用户所输入的字符串进行分析判断,并在你的程序图形界 面上向用户进行相应的提示。 3、在上一个实验(实验三)的第 2 题中,我们设计了一个仿 Windows 系统自带的计算器的界面。但 仅仅要求设计出其界面,而没有要求进行事件处理。在本次实验中,请实现其事件处理。使得程序运行 之后,各项主要运算功能能良好运行。请自己输入数据进行测试。并尽量实现对非法数据的判断和处理。
四、代码
package 图形界面程序;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame {
private JLabel titleLabel;
private JLabel numberOneLabel;
private JLabel numberTwoLabel;
private JLabel resultLabel;
private JTextField numberOneTextField;
private JTextField numberTwoTextField;
private JTextField resultTextField;
private JButton addButton;
private JButton subtractButton;
private JButton clearButton;
public Calculator() {
// 设置窗口标题和大小
setTitle("简易计算器");
setSize(800, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(6, 3));
// 创建各个组件
titleLabel = new JLabel("简易计算器");
numberOneLabel = new JLabel("运算数一:");
numberTwoLabel = new JLabel("运算数二:");
resultLabel = new JLabel("运算结果:");
numberOneTextField = new JTextField();
numberTwoTextField = new JTextField();
resultTextField = new JTextField();
addButton = new JButton("相加");
subtractButton = new JButton("相减");
clearButton = new JButton("全部清零");
// 添加组件到窗口
add(titleLabel);
add(new JLabel());
add(numberOneLabel);
add(numberOneTextField);
add(numberTwoLabel);
add(numberTwoTextField);
add(resultLabel);
add(resultTextField);
add(addButton);
add(subtractButton);
add(clearButton);
// 为按钮添加点击事件监听器
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calculate('+');
}
});
subtractButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calculate('-');
}
});
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numberOneTextField.setText("");
numberTwoTextField.setText("");
resultTextField.setText("");
}
});
setVisible(true);
}
// 计算结果
private void calculate(char operator) {
String num1 = numberOneTextField.getText();
String num2 = numberTwoTextField.getText();
// 检查运算数是否输入
if (num1.isEmpty() || num2.isEmpty()) {
JOptionPane.showMessageDialog(this, "请输入运算数", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
// 检查运算数是否合法
if (!isNumber(num1) || !isNumber(num2)) {
JOptionPane.showMessageDialog(this, "请输入合法的数值", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
// 将字符串转换为浮点数
double number1 = Double.parseDouble(num1);
double number2 = Double.parseDouble(num2);
double result = 0;
if (operator == '+') {
result = number1 + number2;
} else if (operator == '-') {
result = number1 - number2;
}
// 显示结果
resultTextField.setText(String.valueOf(result));
}
// 判断字符串是否为合法的数值
private boolean isNumber(String str) {
// 检查是否包含非法字符
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c != '.' && (c < '0' || c > '9')) {
return false;
}
}
// 检查小数点个数和位置
if (str.indexOf('.') != str.lastIndexOf('.') ||
str.startsWith(".") || str.endsWith(".")) {
return false;
}
return true;
}
public static void main(String[] args) {
new Calculator();
}
}