Java课设——考试系统客户端

做的比较简陋,只能完成最基本的要求T-T。而且一点网络安全意识都没有涉及,防注入没来得及做,密码也没有加密存储>_<。

课设要求

可支持单选,多选和多行填空题,在一屏显示所有试题和解答控件,分左右两边显示,左边显示考试试题,右边显示解答控件。试题内容从数据库读取。交卷给出得分。

感觉老师要求的界面是这样的:

设计思路

我的文件们是这样排布的。(其实我也想知道一个正经客户端软件是怎么设计的,三天时间从0.01开始学java实在不够我浪的,只能自己瞎摸索……)

登录页面

user.java定义了User类来存储usernamepassword两种数据类型。GetUser定义的类对从数据库中取到usernamepassword转换成User类进行存储。usercheck.java将用户从LoginFrame输入的值与从数据库中读取到的进行对比,成功则进入系统,失败则重新输入。

试题读取与显示

Question.java定义的Question类用来存储题干和选项。题目在数据库中用了三张表进行存储,多选题multiplechoice,多行填空multiplefillin和单选题singlechoice,所以也设计了三个java文件进行读取与转换格式,读取的数据存入List<Question>结果集当中。getAllQuestion()方法用于在考试界面MainFrame中输出试题。

答案显示

Answers.javaQuestion.java一样,只不过把question类换成了answers类。

做题模块

添加了计时器功能。

数据库设计


单选题

多选题

多行填空

答案

代码

数据库连接模块

SQLConnection.java

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
//连接数据库
package examsystem;
import java.sql.*;

public class SQLConnectin{

String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/examsystem?serverTimezone=UTC";
Connection conn = null;
Statement stmt = null;

String USER="root";
String PASS="root";

ResultSet rs;
public SQLConnectin() throws Exception{
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
}
catch(SQLException se) {
se.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}

public ResultSet executeQuery(String sql) throws Exception{
rs = stmt.executeQuery(sql);
return rs;
}

}

用户登录模块

User.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package examsystem;

public class User{
private String username;
private String passwd;

public User(){
}

public User(String username,String passwd) {
this.username = username;
this.passwd = passwd;
}

public String getusername() {
return username;
}

public String getpasswd() {
return passwd;
}
}

UserCheck.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package examsystem;

public class UserCheck{
private GetUser getuser;

public String login(String username,String passwd) throws Exception{
getuser = new GetUser();
User user = getuser.singleuser(username);
if(user != null) {
if(user.getpasswd().equals(passwd))
return "登录成功!";
}
return "登录失败!";
}
}

GetUser.java

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
package examsystem;
import java.io.*;
import java.sql.*;

public class GetUser{
User user;
SQLConnectin conn;
ResultSet rs;
String name,passwd;

public User singleuser(String username) throws Exception{
user = null;
try {
String sqlstring = "select username,passwd from user_info";
try {
conn = new SQLConnectin();
rs = conn.executeQuery(sqlstring);
while(rs.next()){
name = rs.getString("username");
passwd = rs.getString("passwd");
if(name.equals(username)) {
user = new User(name,passwd);
break;
}
}
}
catch(SQLException se) {
se.printStackTrace();
}
}
catch (IOException e) {
e.printStackTrace();
}
return user;
}
}

LoginFrame.java

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package examsystem;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginFrame extends JFrame implements ActionListener,MouseListener {
private static final long serialVersionUID = 1L;

String username;
private JTextField usernameText;
private JPasswordField passwordText;
private JButton loginButton;
private JButton cancelButton;
private JLabel usernameLabel;
private JLabel passwordLabel;

public LoginFrame(String name) {
super(name);
this.setSize(600, 400);
this.setUndecorated(true);
this.setLocationRelativeTo(null);

usernameLabel = new JLabel("username");
usernameLabel.setBounds(10, 100, 90, 35);
usernameLabel.setForeground(Color.white);
usernameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
this.add(usernameLabel);

usernameText = new JTextField();
usernameText.setBounds(120, 100, 150, 35);
usernameText.setFont(new Font("微软雅黑",Font.PLAIN,16));
usernameText.setForeground(Color.white);
usernameText.setOpaque(false);
usernameText.setBorder(null);
usernameText.addMouseListener(this);
//usernameText.setForeground(Color.white);
this.add(usernameText);

passwordLabel = new JLabel("password");
passwordLabel.setBounds(10, 150, 90, 35);
passwordLabel.setForeground(Color.white);
passwordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
this.add(passwordLabel);

passwordText = new JPasswordField();
passwordText.setBounds(120, 150, 150, 35);
passwordText.setForeground(Color.white);
passwordText.setOpaque(false);
passwordText.setBorder(null);
passwordText.addMouseListener(this);
//usernameText.setForeground(Color.white);
this.add(passwordText);

loginButton = new JButton("Login");
loginButton.setFont(new Font("微软雅黑",Font.PLAIN,18));
loginButton.setBounds(20, 250, 100, 35);
loginButton.setForeground(Color.white);
loginButton.setOpaque(false);
loginButton.setContentAreaFilled(false);
loginButton.addActionListener(this);
this.add(loginButton);

cancelButton = new JButton("Cancel");
cancelButton.setFont(new Font("微软雅黑",Font.PLAIN,18));
cancelButton.setBounds(150, 250, 100, 35);
cancelButton.setOpaque(false);
cancelButton.setContentAreaFilled(false);
cancelButton.setForeground(Color.white);
this.add(cancelButton);
cancelButton.addActionListener(this);
this.add(new LoginPanel());
this.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if(e.getSource() == cancelButton) {
JOptionPane.showMessageDialog(this, "welcome again");
System.exit(0);
}
if(e.getSource() == loginButton) {
username = usernameText.getText();
String passwd = String.valueOf(passwordText.getPassword());

UserCheck usercheck = new UserCheck();
String result;
try {
result = usercheck.login(username, passwd);
if(result.equals("登录成功!")) {
setVisible(false);
JOptionPane.showMessageDialog(null, "登录成功", "用户提示", JOptionPane.WARNING_MESSAGE);
new MainFrame("考试界面");
}
else {
JOptionPane.showMessageDialog(null, "用户名或密码不正确", "用户提示", JOptionPane.WARNING_MESSAGE);
usernameText.setText("");
passwordText.setText("");
}
} catch (Exception e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}

}
}

@Override
public void mouseClicked(MouseEvent e) {
// TODO 自动生成的方法存根

}

@Override
public void mouseEntered(MouseEvent e) {
if(e.getSource()==usernameText)
{
usernameText.setBorder(BorderFactory.createLineBorder(new Color(255,255,255)));
}else if(e.getSource()==passwordText)
{
passwordText.setBorder(BorderFactory.createLineBorder(new Color(255,255,255)));
}

}

@Override
public void mouseExited(MouseEvent e) {
// TODO 自动生成的方法存根
if(e.getSource()==usernameText)
{
usernameText.setBorder(null);
}else if(e.getSource()==passwordText)
{
passwordText.setBorder(null);
}
}

@Override
public void mousePressed(MouseEvent e) {
// TODO 自动生成的方法存根

}

@Override
public void mouseReleased(MouseEvent e) {
// TODO 自动生成的方法存根

}
}

LoginPanel.java

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
package examsystem;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class LoginPanel extends JPanel {
private Image image;
public LoginPanel()
{
try {
image= ImageIO.read(new File("image/login1.png"));
} catch (IOException e) {
e.printStackTrace();
}

}

@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(image,0,0,600,400,null);
}
}

试题显示模块

Question.java

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
package examsystem;

public class Question{
private String QuestionStem;
private String ChoiceA;
private String ChoiceB;
private String ChoiceC;
private String ChoiceD;
//private String answer; // 用来存储答案

public Question() {
}
//咱所有题都没有图片,我没时间做数据库存图了。
public Question(String QuestionStem, String ChoiceA,String ChoiceB,String ChoiceC,String ChoiceD) {
this.QuestionStem = QuestionStem;
this.ChoiceA = ChoiceA;
this.ChoiceB = ChoiceB;
this.ChoiceC = ChoiceC;
this.ChoiceD = ChoiceD;
}

public String getStem() {
return QuestionStem;
}

public String getChoiceA() {
return ChoiceA;
}

public String getChoiceB() {
return ChoiceB;
}

public String getChoiceC() {
return ChoiceC;
}

public String getChoiceD() {
return ChoiceD;
}

@Override
public String toString() {
return "Question{" +
"QuestionStem='" + QuestionStem + '\'' +
", ChoiceA='" + ChoiceA + '\'' +
", ChoiceB='" + ChoiceB + '\'' +
", ChoiceC='" + ChoiceC + '\'' +
", ChoiceD='" + ChoiceD + '\'' +
'}';
}
}

Multichoice.java

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
package examsystem;

import java.io.*;
import java.util.*;
import java.sql.*;

public class Multichoice{
SQLConnectin conn;
ResultSet rs;
int QuestionID;
String QuestionStem,ChoiceA,ChoiceB,ChoiceC,ChoiceD;
Question question;

public List<Question> getAllQuestion() throws Exception{
List<Question> list = new LinkedList<>();
question = null;
try {
String sqlstring1 = "select * from multiplechoice";
try {
conn = new SQLConnectin();
rs = conn.executeQuery(sqlstring1);
while(rs.next()) {
QuestionID = rs.getInt("QuestionID");
QuestionStem = rs.getString("QuestionStem");
ChoiceA = rs.getString("ChoiceA");
ChoiceB = rs.getString("ChoiceB");
ChoiceC = rs.getString("ChoiceC");
ChoiceD = rs.getString("ChoiceD");

question = new Question(QuestionStem,ChoiceA,ChoiceB,ChoiceC,ChoiceD);
list.add(question);
}
}
catch(SQLException se) {
se.printStackTrace();
}
}
catch (IOException e) {
e.printStackTrace();
}
return list;
}
}

Singlechoice.java

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
package examsystem;

import java.io.*;
import java.util.*;
import java.sql.*;

public class Singlechoice{
//private HashSet<Question> questionBox = new HashSet<>();
SQLConnectin conn;
ResultSet rs;
int QuestionID;
String QuestionStem,ChoiceA,ChoiceB,ChoiceC,ChoiceD;
Question question;

public List<Question> getAllQuestion() throws Exception{
List<Question> list = new LinkedList<>();
question = null;
try {
String sqlstring1 = "select * from singlechoice";
try {
conn = new SQLConnectin();
rs = conn.executeQuery(sqlstring1);
while(rs.next()) {
QuestionID = rs.getInt("QuestionID");
QuestionStem = rs.getString("QuestionStem");
ChoiceA = rs.getString("ChoiceA");
ChoiceB = rs.getString("ChoiceB");
ChoiceC = rs.getString("ChoiceC");
ChoiceD = rs.getString("ChoiceD");

question = new Question(QuestionStem,ChoiceA,ChoiceB,ChoiceC,ChoiceD);
list.add(question);
}
}
catch(SQLException se) {
se.printStackTrace();
}
}
catch (IOException e) {
e.printStackTrace();
}
return list;
}
}

multifillin.java

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
package examsystem;

import java.io.*;
import java.util.*;
import java.sql.*;

public class Multifillin{
SQLConnectin conn;
ResultSet rs;
int QuestionID;
String QuestionStem;
Question question;

public List<Question> getAllQuestion() throws Exception{
List<Question> list = new LinkedList<>();
question = null;
try {
String sqlstring1 = "select * from multiplefillin";
try {
conn = new SQLConnectin();
rs = conn.executeQuery(sqlstring1);
while(rs.next()) {
QuestionID = rs.getInt("QuestionID");
QuestionStem = rs.getString("QuestionStem");

question = new Question(QuestionStem,null,null,null,null);
list.add(question);
}
}
catch(SQLException se) {
se.printStackTrace();
}
}
catch (IOException e) {
e.printStackTrace();
}
return list;
}
}

答案显示

Answers.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package examsystem;

public class Answers{
private String answer;

public Answers() {
}
public Answers(String answer) {
this.answer = answer;
}

public String getAnswer() {
return answer;
}

@Override
public String toString() {
return "Answer{" +
"answer='" + answer + '\'' +
'}';
}
}

GetAnswers.java

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
package examsystem;

import java.io.*;
import java.util.*;
import java.sql.*;

public class GetAnswers{
SQLConnectin conn;
ResultSet rs;
String answer;
Answers answers;

public List<Answers> getAllAnswers() throws Exception{
List<Answers> list = new LinkedList<>();
answers = null;
try {
String sqlstring1 = "select GROUP_CONCAT(answer) from answers GROUP BY QuestionID";
try {
conn = new SQLConnectin();
rs = conn.executeQuery(sqlstring1);
while(rs.next()) {
answer = rs.getString("GROUP_CONCAT(answer)");
answers = new Answers(answer);
list.add(answers);
}
}
catch(SQLException se) {
se.printStackTrace();
}
}
catch (IOException e) {
e.printStackTrace();
}
return list;
}
}

答题界面

MainFrame.java

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package examsystem;

import java.util.*;
import java.util.List;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MainFrame extends JFrame { // 考试界面
private static final long serialVersionUID = 1L;
//private int current = 0;//记录当前题号
private String[] answers_singlechoice = new String[4];//存储考生单选题答案的数组
private String[] real_answers_singlechoice = new String[4];//存储正确单选题答案的数组

private String[][] answers_multiplechoice = new String[3][4];//存储考生多选题答案的数组
private String[] real_answers_multiplechoice = new String[3];//存储正确多选题答案的数组

private String[] real_answers_multifillin = new String[3];//存储正确填空题的数组,用换行符分割,然后从数据库检查对比

private int[] flag = new int[11];//判断题目是否被作答

private JPanel ExamTextPanel = new JPanel();
private JPanel ChosePanel = new JPanel();

private JLabel timeLabel;
private JLabel realTimeLabel = new JLabel();

private JScrollPane scrollPane1;
private JTextArea examArea;

private JButton submitButton;
//一共10道题的panel
private JPanel[] jp = new JPanel[10];
//4道单选题的组件。
private JRadioButton[] asingleButton = new JRadioButton[4];
private JRadioButton[] bsingleButton = new JRadioButton[4];
private JRadioButton[] csingleButton = new JRadioButton[4];
private JRadioButton[] dsingleButton = new JRadioButton[4];
//3道多选题的组件。
private JCheckBox[] amultiButton = new JCheckBox[3];
private JCheckBox[] bmultiButton = new JCheckBox[3];
private JCheckBox[] cmultiButton = new JCheckBox[3];
private JCheckBox[] dmultiButton = new JCheckBox[3];
//一道单选题的按钮组件
private ButtonGroup[] buttongroup = new ButtonGroup[7];
//3道多行填空的文本域组件,通过换行符分割每个空的答案
private JTextArea[] multifillin_textarea = new JTextArea[3];
private int i,j;
private int score = 0;
//计时器
private TimeControlThread timeControlThread = new TimeControlThread();
private int time = 5;
//序号数组
private Label[] jl = new Label[10];

public MainFrame(String s) throws Exception {
// 展示窗口内容
super(s);
int jp_x,jp_y,jp_width,jp_height;//jp数组中的坐标
int choice_x,choice_y,choice_width,choice_height;
int blank_x,blank_y,blank_width,blank_height;
int jl_x;

setBounds(100, 100, 1100, 950);
setResizable(false);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
ExamTextPanel.setLayout(null);
ExamTextPanel.setBounds(0, 0, 1100, 950);
ExamTextPanel.setBackground(Color.LIGHT_GRAY);
timeControlThread.start();

examArea = new JTextArea();
examArea.setEnabled(false); // 文本域不可编辑
examArea.setFont(new Font("黑体", Font.BOLD, 18));
scrollPane1 = new JScrollPane(examArea);
scrollPane1.setBounds(16, 10, 700, 900);
ExamTextPanel.add(scrollPane1);
add(ExamTextPanel);

// 选择面板
ChosePanel.setBounds(750, 10, 300, 900);
ChosePanel.setLayout(null);
ChosePanel.setBorder(BorderFactory.createLineBorder(Color.black));

//一行组件的坐标
jp_x=1;
jp_y=1;
jp_width=298;
jp_height=35;
//一个选项按钮的坐标
choice_x = 40;
choice_y = 10;
choice_width = 50;
choice_height = 30;
//一道多行填空的坐标
blank_x=240;
//blank_y=;
blank_width=200;
blank_height=150;

jl_x = 20;
for(i=0;i<jp.length;i++) {
jp[i] = new JPanel();

if(i<7) {
buttongroup[i] = new ButtonGroup();
jp[i].setBounds(jp_x,jp_y,jp_width,jp_height);
if(i<4) {
asingleButton[i] = new JRadioButton("A");
bsingleButton[i] = new JRadioButton("B");
csingleButton[i] = new JRadioButton("C");
dsingleButton[i] = new JRadioButton("D");
jl[i] = new Label(String.valueOf(i+1)+"、"+" ");

asingleButton[i].setBounds(choice_x,choice_y,choice_width,choice_height);
bsingleButton[i].setBounds(choice_x+50,choice_y,choice_width,choice_height);
csingleButton[i].setBounds(choice_x+100,choice_y,choice_width,choice_height);
dsingleButton[i].setBounds(choice_x+150,choice_y,choice_width,choice_height);
jl[i].setBounds(jl_x, choice_y, choice_width, choice_height);

jp[i].add(asingleButton[i]);
jp[i].add(bsingleButton[i]);
jp[i].add(csingleButton[i]);
jp[i].add(dsingleButton[i]);

buttongroup[i].add(asingleButton[i]);
buttongroup[i].add(bsingleButton[i]);
buttongroup[i].add(csingleButton[i]);
buttongroup[i].add(dsingleButton[i]);

ChosePanel.add(jp[i]);
ChosePanel.add(asingleButton[i]);
ChosePanel.add(bsingleButton[i]);
ChosePanel.add(csingleButton[i]);
ChosePanel.add(dsingleButton[i]);
ChosePanel.add(jl[i]);
ExamTextPanel.add(ChosePanel);
}
else if(i>=4) {
amultiButton[i-4] = new JCheckBox("A");
bmultiButton[i-4] = new JCheckBox("B");
cmultiButton[i-4] = new JCheckBox("C");
dmultiButton[i-4] = new JCheckBox("D");

amultiButton[i-4].setBounds(choice_x,choice_y,choice_width,choice_height);
bmultiButton[i-4].setBounds(choice_x+50,choice_y,choice_width,choice_height);
cmultiButton[i-4].setBounds(choice_x+100,choice_y,choice_width,choice_height);
dmultiButton[i-4].setBounds(choice_x+150,choice_y,choice_width,choice_height);

jp[i].add(amultiButton[i-4]);
jp[i].add(bmultiButton[i-4]);
jp[i].add(cmultiButton[i-4]);
jp[i].add(dmultiButton[i-4]);

ChosePanel.add(jp[i]);
ChosePanel.add(amultiButton[i-4]);
ChosePanel.add(bmultiButton[i-4]);
ChosePanel.add(cmultiButton[i-4]);
ChosePanel.add(dmultiButton[i-4]);
ExamTextPanel.add(ChosePanel);

}
jp_y += 30;
choice_y += 30;
jl_x += 30;
}
else if(i>=7) {
jp[i].setBounds(jp_x,jp_y,blank_width,blank_height);

multifillin_textarea[i-7] = new JTextArea(5,10);
multifillin_textarea[i-7].setFont(new Font("黑体", Font.PLAIN, 18));
multifillin_textarea[i-7].setBounds(choice_x,choice_y,blank_width,blank_height);

ChosePanel.add(jp[i]);
ChosePanel.add(multifillin_textarea[i-7]);
ExamTextPanel.add(ChosePanel);

jp_y += 160;
choice_y += 160;
}
}

submitButton = new JButton("提交");
submitButton.setForeground(Color.black);
submitButton.setBounds(90, 850, 100, 30);
ChosePanel.add(submitButton);
submitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//鼠标放到提交按钮时指针变成小手

timeLabel = new JLabel("距离考试结束:");
timeLabel.setForeground(Color.BLUE);
realTimeLabel.setForeground(Color.BLUE);
timeLabel.setBounds(10, 720, 100, 30);
realTimeLabel.setBounds(10, 740, 150, 30);
ChosePanel.add(timeLabel);
ChosePanel.add(realTimeLabel);

ExamTextPanel.add(ChosePanel);

examArea.append(" 一、单项选择题"+"\n");
ShowsinglechoiceQuestion();
examArea.append(" 二、多项选择题"+"\n");
ShowmultiplechoiceQuestion();
examArea.append(" 三、多行填空题"+"\n");
ShowmultifillinQuestion();
for(i=0;i<10;i++) {
if(i<4)
addListener_singleradio(i);
if(i>=4 && i<7)
addListener_checkbox(i);
}
Answer();
addListener_submit();
}
//从数据库查询单选题
private void ShowsinglechoiceQuestion() throws Exception {
Singlechoice Singlechoice = new Singlechoice();
List<Question> allQuestion = Singlechoice.getAllQuestion();
for (Question question : allQuestion) {
String stem = question.getStem();
String ChoiceA = question.getChoiceA();
String ChoiceB = question.getChoiceB();
String ChoiceC = question.getChoiceC();
String ChoiceD = question.getChoiceD();
examArea.append(" "+stem+"\n"+" "+ChoiceA+"\n"+" "+ChoiceB+"\n"+" "+ChoiceC+"\n"+" "+ChoiceD+"\n"+"\n");
}
}
//从数据库查询多选题
private void ShowmultiplechoiceQuestion() throws Exception{
Multichoice Multichoice = new Multichoice();
List<Question> allQuestion = Multichoice.getAllQuestion();
for(Question question : allQuestion) {
String stem = question.getStem();
String ChoiceA = question.getChoiceA();
String ChoiceB = question.getChoiceB();
String ChoiceC = question.getChoiceC();
String ChoiceD = question.getChoiceD();
examArea.append(" "+stem+"\n"+" "+ChoiceA+"\n"+" "+ChoiceB+"\n"+" "+ChoiceC+"\n"+" "+ChoiceD+"\n"+"\n");
}
}
//从数据库查询多行填空题
private void ShowmultifillinQuestion() throws Exception{
Multifillin Multifillin = new Multifillin();
List<Question> allQuestion = Multifillin.getAllQuestion();
for(Question question : allQuestion) {
String stem = question.getStem();
examArea.append(" "+stem+"\n"+"\n");
}
}
//查询答案
private void Answer() throws Exception{
GetAnswers GetAnswers = new GetAnswers();
List<Answers> allAnswer = GetAnswers.getAllAnswers();
i=0;
for(Answers answer : allAnswer) {
String answer1 = answer.getAnswer();
if(i<4)
real_answers_singlechoice[i] = answer1;
if(i>=4 && i<7)
real_answers_multiplechoice[i-4] = answer1;
if(i>=7 && i<10)
real_answers_multifillin[i-7] = answer1;
i++;
}
}

private class TimeControlThread extends Thread {
public void run() {
while (time >= 0) {
int timeh = time / 60 / 60 % 60;
int timem = time / 60 % 60;
int times = time % 60;
time--;
realTimeLabel.setText("还剩" + timeh + "小时" + timem + "分钟" + times + "秒");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
submitButton.setEnabled(false);
int score = MainFrame.this.Score();
examArea.setText("考试已经结束\n您的最终成绩为:" + score);
}
}

//单选题监听器
public void addListener_singleradio(int current) {
ActionListener optionListner = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JRadioButton radiobutton = (JRadioButton) e.getSource();
System.out.print(current+" ");
answers_singlechoice[current] = radiobutton.getText();
System.out.print(answers_singlechoice[current]+" ");
flag[current] = 1;
System.out.print(flag[current]);
System.out.println();
}
};
asingleButton[current].addActionListener(optionListner);
bsingleButton[current].addActionListener(optionListner);
csingleButton[current].addActionListener(optionListner);
dsingleButton[current].addActionListener(optionListner);
}
//多选题监听器
public void addListener_checkbox(int current) {
ActionListener optionListner = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print(current+" ");
if(amultiButton[current-4].isSelected()) {
answers_multiplechoice[current-4][0] = amultiButton[current-4].getText();
System.out.print(answers_multiplechoice[current-4][0]);
flag[current] = 1;
}
else {
answers_multiplechoice[current-4][0] = null;
}
if(bmultiButton[current-4].isSelected()) {
answers_multiplechoice[current-4][1] = bmultiButton[current-4].getText();
System.out.print(answers_multiplechoice[current-4][1]);
flag[current] = 1;
}
else {
answers_multiplechoice[current-4][1] = null;
}
if(cmultiButton[current-4].isSelected()) {
answers_multiplechoice[current-4][2] = cmultiButton[current-4].getText();
System.out.print(answers_multiplechoice[current-4][2]);
flag[current] = 1;
}
else {
answers_multiplechoice[current-4][2] = null;
}
if(dmultiButton[current-4].isSelected()) {
answers_multiplechoice[current-4][3] = dmultiButton[current-4].getText();
System.out.print(answers_multiplechoice[current-4][3]);
flag[current] = 1;
}
else {
answers_multiplechoice[current-4][3] = null;
}

//}
System.out.print(answers_multiplechoice[current-4][0]+answers_multiplechoice[current-4][1]+answers_multiplechoice[current-4][2]+answers_multiplechoice[current-4][3]);
System.out.println();
}
};
amultiButton[current-4].addActionListener(optionListner);
bmultiButton[current-4].addActionListener(optionListner);
cmultiButton[current-4].addActionListener(optionListner);
dmultiButton[current-4].addActionListener(optionListner);

}
//提交按钮监听器
public void addListener_submit() {
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int values = JOptionPane.showConfirmDialog(null, "是否确认提交试卷?", "用户提示", JOptionPane.YES_NO_CANCEL_OPTION);
if(values == 0) {
int score = MainFrame.this.Score();
examArea.setText("考试已经结束\n您的最终成绩为:" + score);
}
}
});
}

private int Score() {
score = 0;
String multiplechoicegroup = "";
String real_multiplechoicegroup = "";
//计算单选题
int m=0;
for(i=0;i<10;i++) {
if(i<4) {
if(answers_singlechoice[i] != null)
if(answers_singlechoice[i].equals(real_answers_singlechoice[i]))
score += 10;
else if(answers_singlechoice[i] == null)
score += 0;
}
if(i>=4 && i<7) {
multiplechoicegroup = "";
real_multiplechoicegroup = "";
for(j=0;j<4;j++) {
if(answers_multiplechoice[i-4][j] != null)
multiplechoicegroup += answers_multiplechoice[i-4][j];
}
String[] temp = real_answers_multiplechoice[i-4].split(",");
for(m=0;m<real_answers_multiplechoice[i-4].split(",").length;m++)
real_multiplechoicegroup += real_answers_multiplechoice[i-4].split(",")[m];
if(multiplechoicegroup.equals(real_multiplechoicegroup))
score +=10;
}
if(i>=7 && i<10) {
String a = "";
for(m=0;m<real_answers_multifillin[i-7].split(",").length;m++)
a += real_answers_multifillin[i-7].split(",")[m];
System.out.print(a);
if(Arrays.equals(multifillin_textarea[i-7].getText().split("\\r\\n|\\n|\\r"), real_answers_multifillin[i-7].split(",")))
score += 10;
}
}
submitButton.setEnabled(false);
return score;
}

}

入口

main.java

1
2
3
4
5
6
7
8
9
10
package examsystem;

public class main {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//new MainFrame("考试界面");
new LoginFrame("登录界面");
}

}

效果图

登录界面

答题界面

最终界面

心得

由于上课划水摸鱼严重,做课设的时候压根儿就是两眼一抹黑,完全不知道从哪里下手。从数据库中取题目的时候不知道怎么输出,时间又比较紧(还是平时没有提前准备),路上感觉要挂了,哭了好几次。。像我一样的普通大学生还是要平时好好学习。