-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStdTest.java
More file actions
195 lines (181 loc) · 5.89 KB
/
StdTest.java
File metadata and controls
195 lines (181 loc) · 5.89 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
package KYUTES;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class StdTest implements ActionListener {
public JFrame frame;
private JPanel stdTest;
private JComboBox<String> subjectCB;
private JButton start,confirmAns,previous;
private JLabel timeJL,timer;
private JTextArea question;
private JTextField answer;
CountDown cd;
private int testTime,n,score;
private String userName,test,ans;
/*public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StdTest window = new StdTest(userName);
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}*/
public StdTest(String userName) {
this.userName = userName;
initialize();
}
private void initialize() {
frame = new JFrame("Online Test");
frame.setSize(900, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
BarPanel bar = new BarPanel(frame,userName);
stdTest = new JPanel();
stdTest.setBounds(0, 50, 900, 550);
stdTest.setBackground(new Color(186, 202, 224));
stdTest.setLayout(null);
frame.getContentPane().add(stdTest);
//選擇考試
subjectCB = new JComboBox<String>();
subjectCB.setBounds(200, 50, 150, 23);
subjectCB.setFont(new Font("", Font.PLAIN, 13));
stdTest.add(subjectCB);
try {
Connection conn = UseDatabase.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM `exams`");
subjectCB.addItem("請選擇");
while(rs.next()){
subjectCB.addItem(rs.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
}
subjectCB.addActionListener(this);
//作答時間
timeJL = new JLabel("作答時間: "+testTime+"分鐘");
timeJL.setBounds(200, 100, 150, 20);
timeJL.setFont(new Font("", Font.BOLD, 15));
stdTest.add(timeJL);
//開始考試按鈕
start = new JButton("開始考試");
start.setBounds(350, 100, 90, 23);
start.setFont(new Font("", Font.BOLD, 13));
stdTest.add(start);
start.setEnabled(false);
start.addActionListener(this);
//倒數計時
timer = new JLabel();
timer.setBounds(480, 100, 150, 20);
timer.setFont(new Font("", Font.BOLD, 15));
stdTest.add(timer);
//題目&作答區
question = new JTextArea();
question.setBackground(new Color(240, 240, 240));
question.setBounds(200, 150, 500, 250);
question.setEditable(false);
question.setFont(new Font("", Font.BOLD, 20));
stdTest.add(question);
JLabel answerJL = new JLabel("答案:");
answerJL.setBounds(200, 410, 40, 20);
answerJL.setFont(new Font("", Font.BOLD, 15));
stdTest.add(answerJL);
answer = new JTextField(1);
answer.setBackground(new Color(240, 240, 240));
answer.setBounds(240, 410, 200, 25);
answer.setFont(new Font("", Font.PLAIN, 20));
stdTest.add(answer);
answer.setEditable(false);
confirmAns = new JButton("確定答案");
confirmAns.setBounds(450, 410, 90, 23);
confirmAns.setFont(new Font("", Font.BOLD, 13));
stdTest.add(confirmAns);
confirmAns.setEnabled(false);
confirmAns.addActionListener(this);
previous = new JButton("上一頁");
previous.setBounds(780, 450, 80, 25);
previous.setFont(new Font("", Font.PLAIN, 10));
stdTest.add(previous);
previous.addActionListener(this);
}
void showQuestion() {
try {
Connection conn = UseDatabase.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM "+test+"paper where `question_num` = "+n);
if(rs.next()) { //未完成全部題目
question.setText(rs.getString("quetion"));
ans=rs.getString("ans");
}else {
cd.stop(); //停止倒數
answer.setEditable(false); //不能再撰寫答案
confirmAns.setEnabled(false); //不能送出答案、進入下一題
question.setText("Finish!\r\nScore = "+score+" / 50");
st.executeUpdate("Update user_" + userName + " SET `score` = "+score+" where `subject` = '" + test + "'");
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==previous) {
frame.dispose();
}
if(e.getSource()==subjectCB) {
try {
Connection conn = UseDatabase.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM `exams` where `name`='"+subjectCB.getSelectedItem()+"'");
rs.next();
test = (String) subjectCB.getSelectedItem();
testTime = rs.getInt("time");
timeJL.setText("作答時間: "+testTime+"分鐘");
start.setEnabled(true);
} catch (Exception e1) {
e1.printStackTrace();
}
}
if(e.getSource()==start) {
try {
Connection conn = UseDatabase.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM user_" + userName + " where `subject` = '" + test + "'");
rs.next();
if(rs.getInt("score") != 0 ) { //還沒有測驗過
cd = new CountDown(timer,testTime);
answer.setEditable(true); //開始之後才可以輸入
confirmAns.setEnabled(true);
n=1;
showQuestion();
start.setEnabled(false); //設定按鈕不可點選
cd.start();
} else {
JOptionPane.showMessageDialog(frame, "已進行過測驗");
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
if(e.getSource()==confirmAns) {
if(timer.getText().equals("Time's Over!")) {
question.setText("Time's Over!\r\nScore = "+score+" / 50");
confirmAns.setEnabled(false);
}else {
if(answer.getText().equals(ans)) { //答對
score+=10; //一題10分
}
answer.setText(null);
n++; //題號++
showQuestion();
}
}
}
}