-
Notifications
You must be signed in to change notification settings - Fork 1
/
RadioQuestion.java
executable file
·86 lines (75 loc) · 1.98 KB
/
RadioQuestion.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
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.util.Random;
import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class RadioQuestion extends JPanel implements ActionListener{
int correctAns;
Quiz quiz;
boolean count = false;
boolean wcount = false;
int selected;
boolean used = false;
JPanel qPanel=new JPanel();
JPanel aPanel=new JPanel();
JRadioButton[] responses;
ButtonGroup group=new ButtonGroup();
JPanel botPanel=new JPanel();
JButton next=new JButton("Next");
JButton finish=new JButton("Finish");
public RadioQuestion(String q, String[] options, int ans, Quiz quiz){
this.quiz=quiz;
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
correctAns=ans;
qPanel.add(new JLabel(q));
add(qPanel);
responses=new JRadioButton[options.length];
for(int i=0;i<options.length;i++){
responses[i]=new JRadioButton(options[i]);
responses[i].addActionListener(this);
group.add(responses[i]);
aPanel.add(responses[i]);
}
add(aPanel);
next.addActionListener(this);
finish.addActionListener(this);
botPanel.add(next);
botPanel.add(finish);
add(botPanel);
}
public void actionPerformed(ActionEvent e){
Object src=e.getSource();
if(src.equals(next) ){
showResult();
quiz.next();
}
if(src.equals(finish)){
quiz.showSummary();
}
for(int i=0;i<responses.length;i++){
if(src==responses[i]){
selected=i;
}
}
}
public void showResult(){
String text=responses[selected].getText();
quiz.total++;
if(selected==correctAns && count == false){
quiz.corrects++;
count = true;
}
else if(selected != correctAns && wcount == false)
{
quiz.wrongs++;
wcount = true;
}
}
}