-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStdExercises.java
More file actions
197 lines (184 loc) · 5.82 KB
/
StdExercises.java
File metadata and controls
197 lines (184 loc) · 5.82 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
package KYUTES;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
public class StdExercises implements ActionListener {
public JFrame frame;
private JPanel stdExercises;
private JComboBox<String> subjectCB;
private JButton start,confirmAns,previous;
private JLabel timeJL,timer;
private JTextArea question;
private JTextField answer;
CountDown cd;
private int testTime,score;
private String userName,test,ans;
ArrayList<Integer> select;
/*public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StdExercises window = new StdExercises(userName);
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}*/
public StdExercises(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);
stdExercises = new JPanel();
stdExercises.setBounds(0, 50, 900, 550);
stdExercises.setBackground(new Color(186, 202, 224));
stdExercises.setLayout(null);
frame.getContentPane().add(stdExercises);
//選擇考試
subjectCB = new JComboBox<String>();
subjectCB.setBounds(200, 50, 150, 23);
subjectCB.setFont(new Font("", Font.PLAIN, 13));
stdExercises.add(subjectCB);
subjectCB.addItem("請選擇科目");
try {
Connection conn = UseDatabase.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM user_"+userName);
while(rs.next()){
subjectCB.addItem(rs.getString("subject"));
}
} catch (Exception e) {
e.printStackTrace();
}
subjectCB.addActionListener(this);
//作答時間
testTime = 3;
timeJL = new JLabel("作答時間: 3分鐘");
timeJL.setBounds(200, 100, 150, 20);
timeJL.setFont(new Font("", Font.BOLD, 15));
stdExercises.add(timeJL);
//開始考試按鈕
start = new JButton("開始考試");
start.setBounds(350, 100, 90, 23);
start.setFont(new Font("", Font.BOLD, 13));
stdExercises.add(start);
start.setEnabled(false);
start.addActionListener(this);
//倒數計時
timer = new JLabel();
timer.setBounds(480, 100, 150, 20);
timer.setFont(new Font("", Font.BOLD, 15));
stdExercises.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));
stdExercises.add(question);
JLabel answerJL = new JLabel("答案:");
answerJL.setBounds(200, 410, 40, 20);
answerJL.setFont(new Font("", Font.BOLD, 15));
stdExercises.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));
stdExercises.add(answer);
answer.setEditable(false);
confirmAns = new JButton("確定答案");
confirmAns.setBounds(450, 410, 90, 23);
confirmAns.setFont(new Font("", Font.BOLD, 13));
stdExercises.add(confirmAns);
confirmAns.setEnabled(false);
confirmAns.addActionListener(this);
previous = new JButton("上一頁");
previous.setBounds(780, 450, 80, 25);
previous.setFont(new Font("", Font.PLAIN, 10));
stdExercises.add(previous);
previous.addActionListener(this);
}
void randomTest() {
if(select.size()==5) {
cd.stop(); //停止倒數
answer.setEditable(false); //不能再撰寫答案
confirmAns.setEnabled(false); //不能送出答案、進入下一題
question.setText("Finish!\r\nScore = "+score+" / 50");
}else {
try {
Connection conn = UseDatabase.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select count(`question_num`) from `mathQ`");
rs.next();
int n = rs.getInt(1);
int randomNum=0;
while(true) {
randomNum = (int)(Math.random()*n)+1;
if(!select.contains(randomNum)) {
break;
}
}
select.add(randomNum);
rs = st.executeQuery("select * from `mathQ` where `question_num` = "+randomNum);
rs.next();
//MySQL table裡的question打錯
question.setText(rs.getString("quetion"));
ans = rs.getString("ans");
} catch (Exception e) {
e.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 `user_"+userName+"` where `subject`=\""+subjectCB.getSelectedItem()+"\"");
rs.next();
test = subjectCB.getSelectedItem()+"Paper";
start.setEnabled(true);
} catch (Exception e1) {
e1.printStackTrace();
}
}
if(e.getSource()==start) {
cd = new CountDown(timer,testTime);
answer.setEditable(true); //開始之後才可以輸入
confirmAns.setEnabled(true);
score=0;
select = new ArrayList<Integer>();
this.randomTest();
cd.start();
}
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);
this.randomTest();
}
}
}
}