-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuizApp
107 lines (91 loc) · 3.62 KB
/
QuizApp
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class QuizApp extends JFrame {
private JLabel questionLabel;
private JRadioButton[] optionButtons;
private JButton nextButton;
private JButton finishButton;
private String[] questions = {
"What is the capital of France?",
"Which planet is known as the Red Planet?",
"Who wrote 'Romeo and Juliet'?"
};
private String[][] options = {
{"Paris", "London", "Berlin", "Rome"},
{"Mars", "Jupiter", "Venus", "Saturn"},
{"William Shakespeare", "Charles Dickens", "Jane Austen", "Mark Twain"}
};
private String[] answers = {
"Paris", "Mars", "William Shakespeare"
};
private int currentQuestionIndex = 0;
private int score = 0;
public QuizApp() {
setTitle("Quiz App");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel mainPanel = new JPanel(new BorderLayout());
questionLabel = new JLabel(questions[currentQuestionIndex]);
mainPanel.add(questionLabel, BorderLayout.NORTH);
JPanel optionsPanel = new JPanel(new GridLayout(0, 1));
ButtonGroup optionGroup = new ButtonGroup();
optionButtons = new JRadioButton[4];
for (int i = 0; i < 4; i++) {
optionButtons[i] = new JRadioButton(options[currentQuestionIndex][i]);
optionGroup.add(optionButtons[i]);
optionsPanel.add(optionButtons[i]);
}
mainPanel.add(optionsPanel, BorderLayout.CENTER);
nextButton = new JButton("Next");
nextButton.addActionListener(new NextButtonListener());
finishButton = new JButton("Finish");
finishButton.addActionListener(new FinishButtonListener());
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(nextButton);
buttonPanel.add(finishButton);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
add(mainPanel);
setVisible(true);
}
private class NextButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (currentQuestionIndex < questions.length - 1) {
checkAnswer();
currentQuestionIndex++;
updateQuestion();
} else {
JOptionPane.showMessageDialog(QuizApp.this, "No more questions. Click Finish to see your score.", "Info", JOptionPane.INFORMATION_MESSAGE);
}
}
}
private class FinishButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
checkAnswer();
JOptionPane.showMessageDialog(QuizApp.this, "Your score: " + score + "/" + questions.length, "Quiz Score", JOptionPane.INFORMATION_MESSAGE);
dispose();
}
}
private void checkAnswer() {
for (int i = 0; i < optionButtons.length; i++) {
if (optionButtons[i].isSelected() && optionButtons[i].getText().equals(answers[currentQuestionIndex])) {
score++;
break;
}
}
}
private void updateQuestion() {
questionLabel.setText(questions[currentQuestionIndex]);
for (int i = 0; i < optionButtons.length; i++) {
optionButtons[i].setText(options[currentQuestionIndex][i]);
optionButtons[i].setSelected(false);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(QuizApp::new);
}
}