-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction.java
More file actions
329 lines (297 loc) · 8.2 KB
/
Function.java
File metadata and controls
329 lines (297 loc) · 8.2 KB
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
package 軟體工程;
import javax.imageio.ImageIO;//声明导入的类
import java.util.Random;
import java.lang.Math;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.*;
import java.util.Timer;
import java.util.TimerTask;
class Function implements ActionListener {
Date now=new Date();
JButton b1;
JLabel l1;
JFrame f = new JFrame("考試評卷系統");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
// 题号和总分
int index = 0,total = 0;
// 15位数组存储题目编号
int[] QueList = new int[15];
// 设置文件具体路径以读出写入
File file = new File("C:\\Users\\Jacky\\Desktop\\CHINESE.txt");
//File file = new File("D:\\Desktop\\试题库2.txt");//换个试题源
File ans = new File("C:\\Users\\Jacky\\Desktop\\新文字文件(2).txt");
QuestionBank questions[] = new QuestionBank[100];
JLabel questionString = new JLabel("题目:");
JLabel choiceString = new JLabel("选项:");
JLabel totalString = new JLabel("总分:");
JLabel label01=new JLabel("剩下时间");
JLabel correct = new JLabel("答案");
TextArea question = new TextArea("",2,30,2);
JTextField totaltf = new JTextField("0",10);
JRadioButton choiceA = new JRadioButton("A");
JRadioButton choiceB = new JRadioButton("B");
JRadioButton choiceC = new JRadioButton("C");
JRadioButton choiceD = new JRadioButton("D");
ButtonGroup radioGroup = new ButtonGroup();
//向Button里添加文字
JButton beginbt = new JButton("答题开始");
JButton nextbt = new JButton("下个题目");
JButton practiceAgainbt = new JButton("重新开始");
Timer timer = new Timer();
Function()
{
/*
* 系统布局
*/
f.setSize(400,400);
f.setLocation(50,50);
f.setVisible(true);
f.setLayout(new GridLayout(4,1,10,10));
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p2.setLayout(new FlowLayout(FlowLayout.CENTER));
p3.setLayout(new FlowLayout(FlowLayout.CENTER));
p4.setLayout(new FlowLayout(FlowLayout.CENTER));
radioGroup.add(choiceA);
radioGroup.add(choiceB);
radioGroup.add(choiceC);
radioGroup.add(choiceD);
// 添加问题
p1.add(questionString);
p1.add(question);
p1.add(label01);
// 添加选项
p2.add(choiceString);
p2.add(choiceA);
p2.add(choiceB);
p2.add(choiceC);
p2.add(choiceD);
// 添加总分
p3.add(totalString);
p3.add(totaltf);
p3.add(correct);
// 添加三个按钮
p4.add(beginbt);
p4.add(nextbt);
p4.add(practiceAgainbt);
f.add(p1);
f.add(p2);
f.add(p3);
f.add(p4);
nextbt.addActionListener(this);
beginbt.addActionListener(this);
practiceAgainbt.addActionListener(this);
CreatNum();
readFile();
// 使界面为系统风格
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //系统风格
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");//Nimbus风格
}
catch (Exception e)
{
e.printStackTrace();
}
timer.scheduleAtFixedRate(new TimerTask() {
int x = 60;
public void run() {
label01.setText("Time left: " + x);
x--;
if (x < 0) {
timer.cancel();
label01.setText("Time Over");
}
}
}, 0, 1000);}
/*
* 产生随机题号并将它们从大到小排序
*/
void CreatNum(){
for (int i = 0;i<15;i++){
int j = (int)(Math.random()*50);
QueList[i] = j;
}
Arrays.sort(QueList);
}
/*
* 从指定文件中读取题目选项和答案
*/
void readFile()
{
String s,question,choiceA,choiceB,choiceC,choiceD,answer;
try
{
int count=0;
FileInputStream in=new FileInputStream(file);//打开题目文件
byte b[]=new byte[in.available()];
in.read(b);
s=new String(b);
// 分割题目和选项
String c[]=s.split("&&");
for(int i=0;i<c.length;i++)
{
// 分配题目选项和答案
question = c[i++];
choiceA = c[i++];
choiceB = c[i++];
choiceC = c[i++];
choiceD = c[i++];
answer = c[i];
questions[count++] = new QuestionBank(question,choiceA,choiceB,choiceC,choiceD,answer);
}
}catch(Exception e1){ e1.printStackTrace();}
}
/*
* 判断回答是否正确并做出回应,总分累加
*/
void ifRight()
{
// 答案取自题目文件的第五位
// 若正确总分total加一
if(choiceA.isSelected())
{
if(questions[(int)QueList[index-1]].answer.equals(choiceA.getText()))//
{
total+=1;
}
correct.setText(questions[QueList[index-1]].answer);
// 将答题的数据写入指定的文件保存
try {
FileWriter out = new FileWriter(ans, true);
out.write("["+(1+QueList[(index-1)]));
out.write("、A] ");
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(choiceB.isSelected())
{
if(questions[QueList[index-1]].answer.equals(choiceB.getText()))
{
total+=1;
}
correct.setText(questions[QueList[index-1]].answer);
// 将答题的数据写入指定的文件保存
try {
FileWriter out = new FileWriter(ans, true);
out.write("["+(QueList[(index-1)]+1));
out.write("、B] ");
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(choiceC.isSelected())
{
if(questions[QueList[index-1]].answer.equals(choiceC.getText()))
{
total+=1;
}
correct.setText(questions[QueList[index-1]].answer);
// 将答题的数据写入指定的文件保存
try {
FileWriter out = new FileWriter(ans, true);
out.write("["+(1+QueList[(index-1)]));
out.write("、C] ");
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(choiceD.isSelected())
{
if(questions[QueList[index-1]].answer.equals(choiceD.getText()))
{
total+=1;
}
correct.setText(questions[QueList[index-1]].answer);
// 将答题的数据写入指定的文件保存
try {
FileWriter out = new FileWriter(ans, true);
out.write("["+(1+QueList[(index-1)]));
out.write("、D] ");
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 在判断完这一选项是否正确后设置总分
totaltf.setText(Integer.toString(total));
}
/*
* 针对输入的参数polar,将题目、选项设置为第polar位
*/
void setQuestion(int polar)
{
// 设置题目选项
question.setText(questions[polar].question);
choiceA.setText(questions[polar].choiceA);
choiceB.setText(questions[polar].choiceB);
choiceC.setText(questions[polar].choiceC);
choiceD.setText(questions[polar].choiceD);
radioGroup.clearSelection();
}
/*
* 按下各个按钮后的结果
*/
public void actionPerformed(ActionEvent e)
{
// 按下“下个题目”,index加一,跳转到下一题
if(e.getSource() == nextbt)
{
ifRight();
if(index==2) new finish();
if(index<2)
{
setQuestion((int)QueList[index]);
index += 1;
}
}
// 按下“开始答题”,在答题数据文件中写入抬头
if(e.getSource() == beginbt)
{
try {
FileWriter out = new FileWriter(ans, true);
out.write(
"Answer");
out.flush();
out.close();
} catch (IOException x) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
}
// 按下“重新开始”,则返回第一题,分数变为0
if(e.getSource() == practiceAgainbt)
{
index = 0;
total = 0;//分数为0
totaltf.setText(Integer.toString(total));
try {
FileWriter out = new FileWriter(ans, true);
out.write("\n\n");
out.flush();
out.close();
} catch (IOException x) {
// TODO Auto-generated catch block
// x.printStackTrace();
}
}
}
}