-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGUI_Questions.java
311 lines (263 loc) · 9.07 KB
/
GUI_Questions.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
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class GUI_Questions
{
JPanel left = new JPanel();
JPanel right = new JPanel();
JFrame jf5 = new JFrame();
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,left,right);
JLabel Question_tag = new JLabel("Answer The Questions:");
JPanel pane = new JPanel();
JPanel Question = new JPanel();
JLabel Question_stmt = new JLabel();
//buttons of lower right panel
JButton prev = new JButton("Prev");
JButton next = new JButton("Next");
JButton submit = new JButton("Submit");
//Radio Button
JRadioButton option1 = new JRadioButton();
JRadioButton option2 = new JRadioButton();
JRadioButton option3 = new JRadioButton();
JRadioButton option4 = new JRadioButton();
ButtonGroup options = new ButtonGroup();
//Questions_Array
String[][] QuestionList = new String[10][6];
JLabel[] Q = new JLabel[10];
public GUI_Questions(String name,String table) throws SQLException
{
jf5.setTitle(name);
//left.setLayout( new BoxLayout(left,BoxLayout.Y_AXIS));
right.setLayout( new BorderLayout());
jf5.setVisible(true);
jf5.setSize(1300,500);
jf5.setDefaultCloseOperation(jf5.EXIT_ON_CLOSE);
jf5.add(split);
//TAG ADDITION
Question_tag.setFont(new Font("Arial Black",20,40));
pane.setLayout(new BoxLayout(pane,BoxLayout.Y_AXIS));
pane.add(Question_tag);
pane.add(Box.createVerticalStrut(20));
right.add(pane,BorderLayout.CENTER);
//adding question in array
Question_stmt.setFont(new Font("Arial ",10,30));
pane.add(Question_stmt);
Addques(table); // table name is passes from after login page bases on the button which is clicked.
//----------------------------------------------Radiobutton Group------------------------------------------------
options.add(option1);
options.add(option2);
options.add(option3);
options.add(option4);
//pane.add(Box.createVerticalStrut(50));
pane.add(option1);
pane.add(option2);
pane.add(option3);
pane.add(option4);
option1.addActionListener(new Handler());
option2.addActionListener(new Handler());
option3.addActionListener(new Handler());
option4.addActionListener(new Handler());
//----------------------------------------------------leftpane-----------------------------------------------
int j=0;
Q[0] = new JLabel("Ques1 :");
Q[1] = new JLabel("Ques2 :");
Q[2] = new JLabel("Ques3 :");
Q[3] = new JLabel("Ques4 :");
Q[4] = new JLabel("Ques5 :");
Q[5] = new JLabel("Ques6 :");
Q[6] = new JLabel("Ques7 :");
Q[7] = new JLabel("Ques8 :");
Q[8] = new JLabel("Ques9 :");
Q[9] = new JLabel("Ques10 :");
//buttons
JSplitPane split1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
pane.add(split1);
split.setOneTouchExpandable(true);
JPanel button = new JPanel(new FlowLayout());
setQuestion(QuestionList[0][0],QuestionList[0][1],QuestionList[0][2],QuestionList[0][3],QuestionList[0][4]);
button.add(prev);
prev.addActionListener(new Handler());
button.add(next);
next.addActionListener(new Handler());
button.add(submit);
submit.addActionListener(new Handler());
split1.setRightComponent(button);
split1.setDividerLocation(500);
Clocktype clk = new Clocktype(jf5);
button.add(clk.jb);
//clk.jb.setBorder(new EmptyBorder(10,15,20,25));
clk.jb.setBackground(Color.black);
jf5.validate();
// jf5.validate();
}
//setting text for Question
public void setQuestion(String ques,String o1,String o2,String o3,String o4)
{
Question_stmt.setText(ques);
option1.setText(o1);
option2.setText(o2);
option3.setText(o3);
option4.setText(o4);
}
int i = 1;
String[] user_ans = new String[10];
class Handler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== prev)
{
prev.setVisible(true);
next.setVisible(true);
i--;
if(i<0)
{
i =0;
prev.setVisible(false);
}
if(i>=0 && i<=8)
{
setQuestion(QuestionList[i][0],QuestionList[i][1],QuestionList[i][2],QuestionList[i][3],QuestionList[i][4]);
if(user_ans[i]== null)
{ options.clearSelection();}
else
{
if(user_ans[i].equals(option1.getText()))
{option1.setSelected(true);}
else if(user_ans[i].equals(option2.getText()))
{option2.setSelected(true);}
else if(user_ans[i].equals(option3.getText()))
{option3.setSelected(true);}
else if(user_ans[i].equals(option4.getText()))
{option4.setSelected(true);}
}
}
}
if(e.getSource() == next)
{
prev.setVisible(true);
next.setVisible(true);
if(i>=0 && i<=8)
{
i++;
if(i>8)
{
i = 8;
next.setVisible(false);
}
setQuestion(QuestionList[i][0],QuestionList[i][1],QuestionList[i][2],QuestionList[i][3],QuestionList[i][4]);
if(user_ans[i]== null)
{ options.clearSelection();}
else
{
if(user_ans[i].equals(option1.getText()))
{option1.setSelected(true);}
else if(user_ans[i].equals(option2.getText()))
{option2.setSelected(true);}
else if(user_ans[i].equals(option3.getText()))
{option3.setSelected(true);}
else if(user_ans[i].equals(option4.getText()))
{option4.setSelected(true);}
}
}
}
if(e.getSource()==option1)
{
user_ans[i] = option1.getText();
Q[i].setText(Q[i].getText());
}
else if(e.getSource()==option2)
{
user_ans[i] = option2.getText();
Q[i].setText(Q[i].getText());
}
else if(e.getSource()==option3)
{
user_ans[i] = option3.getText();
Q[i].setText(Q[i].getText());
}
else if(e.getSource()==option4)
{
user_ans[i] = option4.getText();
Q[i].setText(Q[i].getText());
}
else if(e.getSource() == submit)
{
Displayscore();
jf5.setEnabled(false);
jf5.setVisible(false);
}
}
}
//---------------------adding random questions and its option and answer in array-----------------------
int j = 0;
void Addques(String table) throws SQLException
{
Random index = new Random();
HashSet<Integer> index_set = new HashSet<Integer>();
index_set.clear();
while(index_set.size()!=10)
{
index_set.add(index.nextInt(11)+1);
}
Iterator<Integer> iterate = index_set.iterator();
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Multiple_Choice_Quiz_System","root","");
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery("select * from "+table);
rs.next();
i=0;
j=0;
while(rs.isAfterLast()!=true && j<=9)
{
if(index_set.contains(rs.getInt("q_id")))
{
QuestionList[j][0] = rs.getString("q_des");
QuestionList[j][1] = rs.getString("opt_1");
QuestionList[j][2] = rs.getString("opt_2");
QuestionList[j][3] = rs.getString("opt_3");
QuestionList[j][4] = rs.getString("opt_4");
QuestionList[j][5] = rs.getString("ans");
j++;
}
rs.next();
}
}
catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
}
int correct_ans;
int wrong_ans;
int not_attempted;
int final_score;
void Displayscore()
{
correct_ans = 0;
wrong_ans = 0;
not_attempted = 0;
final_score = 0;
for(i=0;i<9;i++)
{
if(QuestionList[i][5].equals(user_ans[i]))
{
correct_ans++;
}
else if(user_ans[i]==null)
{
not_attempted++;
}
else
{
wrong_ans++;
}
}
final_score = correct_ans*10;
Gui_Result page7 = new Gui_Result(Integer.toString(correct_ans),Integer.toString(wrong_ans),Integer.toString(not_attempted),Integer.toString(final_score));
}
}