Java小实验-初步使用Swing

Java小实验-初步使用Swing

Swing是Java的GUI工具包,它是Java基础类的一部分。Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表。Swing提供许多比AWT更好的屏幕显示元素。它们用纯Java写成,所以同Java本身一样可以跨平台运行,这一点不像AWT。Swing是JFC的一部分。Swing支持可更换的面板和主题,然而不是真的使用原生平台提供的设备,而是仅仅在表面上模仿它们。这意味着你可以在任意平台上使用JAVA支持的任意面板。Swing提供了一组丰富的GUI组件,如按钮、文本框、复选框、列表框等,以及容器组件,如面板和框架,用于构建交互式应用程序的用户界面。

实验内容

1、完成图1显示的窗体。

图1 学生成绩输入窗体

2、单击“提交”按钮,会将姓名、Java文本框中的内容写入到scores.txt中,写入形式如下:
王明 95
刘小林 75

3、单击“重写”按钮,会清空姓名、Java文本框中的内容。

4、要求必须处理程序中可能出现的所有异常。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import javax.swing.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
public static void main(String[] args) {
// 创建一个JFrame对象,用于显示窗口
JFrame frame = new JFrame("学生成绩输入");
// 设置窗口的大小
frame.setSize(300, 150);
// 设置窗口的关闭操作
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// 创建一个JPanel对象,用于添加组件
JPanel panel = new JPanel();
// 将JPanel对象添加到JFrame对象中
frame.add(panel);
// 调用placeComponents方法,用于添加组件
placeComponents(panel);

// 设置窗口可见
frame.setVisible(true);
}

private static void placeComponents(JPanel panel) {
// 设置布局管理器
panel.setLayout(null);

// 创建一个JLabel对象,用于显示用户名
JLabel userLabel = new JLabel("用户名:");
// 设置JLabel的位置
userLabel.setBounds(10, 20, 80, 25);
// 将JLabel添加到JPanel中
panel.add(userLabel);

// 创建一个JTextField对象,用于显示用户名
JTextField userText = new JTextField(20);
// 设置JTextField的位置
userText.setBounds(100, 20, 165, 25);
// 将JTextField添加到JPanel中
panel.add(userText);

// 创建一个JLabel对象,用于显示成绩
JLabel scoreLabel = new JLabel("清除:");
// 设置JLabel的位置
scoreLabel.setBounds(10, 50, 80, 25);
// 将JLabel添加到JPanel中
panel.add(scoreLabel);

// 创建一个JTextField对象,用于显示成绩
JTextField scoreText = new JTextField(20);
// 设置JTextField的位置
scoreText.setBounds(100, 50, 165, 25);
// 将JTextField添加到JPanel中
panel.add(scoreText);

// 创建一个JButton对象,用于提交数据
JButton submitButton = new JButton("提交");
// 设置JButton的位置
submitButton.setBounds(10, 80, 80, 25);
// 将JButton添加到JPanel中
panel.add(submitButton);

// 创建一个JButton对象,用于清除输入框中的数据
JButton clearButton = new JButton("清除");
// 设置JButton的位置
clearButton.setBounds(180, 80, 80, 25);
// 将JButton添加到JPanel中
panel.add(clearButton);

// 为提交按钮添加点击事件
submitButton.addActionListener(e -> {
// 获取输入框中的用户名和成绩
final var name = userText.getText();
final var score = scoreText.getText();

// 检查输入框中的数据是否合法
if (checkFormat(name, score)) {
try {
// 将用户名和成绩写入文件
writeToFile(name, name);
// 弹出提示框
JOptionPane.showMessageDialog(panel, "写入成功!");
} catch (IOException e1) {
// 弹出提示框
JOptionPane.showMessageDialog(panel, "写入文件失败!");
}
} else {
// 弹出提示框
JOptionPane.showMessageDialog(panel, "用户名或者成绩输入不合法");
}
});

// 为清除按钮添加点击事件
clearButton.addActionListener(e -> {
// 将输入框中的数据清空
userText.setText("");
scoreText.setText("");
});
}

private static boolean checkFormat(String username, String score) {
// 检查输入框中的数据是否合法
if(username.isEmpty() || score.isEmpty()){
return false;
}
// 检查输入框中的数据是否是数字
for (int i = 0; i < score.length(); i++) {
if (!Character.isDigit(score.charAt(i))) {
return false;
}
}
return true;
}

private static void writeToFile(String username, String score) throws IOException {
// 将用户名和成绩写入文件
FileWriter fileWriter = new FileWriter("scores.txt",true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.append("用户名:").append(username).append("\n");
bufferedWriter.append("成绩:").append(score).append("\n");
bufferedWriter.close();
}
}

运行效果

运行结果

运行结果


Java小实验-初步使用Swing
http://cvrain.cloudvl.cn/2023/12/18/Java/java-example-4/
作者
ClaudeRainer
发布于
2023年12月18日
许可协议