-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuizApp.java
More file actions
157 lines (133 loc) · 5.34 KB
/
QuizApp.java
File metadata and controls
157 lines (133 loc) · 5.34 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
package example;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class QuizApp {
private JFrame frame;
private JPanel loginPanel, quizPanel;
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton, submitButton;
private JLabel questionLabel;
private JRadioButton[] optionButtons;
private ButtonGroup optionGroup;
private int currentQuestion = 0;
private int score = 0;
private String[][] questions = {
{"What is the capital of France?", "Berlin", "Madrid", "Paris", "Rome", "Paris"},
{"Which planet is known as the Red Planet?", "Earth", "Mars", "Jupiter", "Saturn", "Mars"},
{"What is the pink city of India?", "Hyderabad", "Mumbai", "Jaipur", "Lucknow", "Jaipur"},
{"What is the capital city of South Korea?", "Bangkok", "Seoul", "Beijing", "Tokyo", "Seoul"}
};
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try {
QuizApp app = new QuizApp();
app.createUI();
} catch (Exception e) {
e.printStackTrace();
}
});
}
public void createUI() {
frame = new JFrame("Quiz App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 400);
frame.setLayout(new CardLayout());
// Login Panel
loginPanel = new JPanel();
loginPanel.setLayout(new GridLayout(4, 2, 10, 10));
loginPanel.setBackground(Color.CYAN);
JLabel usernameLabel = new JLabel("Username:");
usernameField = new JTextField(15);
JLabel passwordLabel = new JLabel("Password:");
passwordField = new JPasswordField(15);
loginButton = new JButton("Login");
loginPanel.add(usernameLabel);
loginPanel.add(usernameField);
loginPanel.add(passwordLabel);
loginPanel.add(passwordField);
loginPanel.add(new JLabel()); // Empty label for spacing
loginPanel.add(new JLabel()); // Empty label for spacing
loginPanel.add(loginButton);
frame.add(loginPanel, "Login");
// Quiz Panel
quizPanel = new JPanel();
quizPanel.setLayout(new BoxLayout(quizPanel, BoxLayout.Y_AXIS));
questionLabel = new JLabel("");
questionLabel.setFont(new Font("Arial", Font.PLAIN, 16));
quizPanel.add(questionLabel);
optionButtons = new JRadioButton[4];
optionGroup = new ButtonGroup();
for (int i = 0; i < 4; i++) {
optionButtons[i] = new JRadioButton();
optionButtons[i].setFont(new Font("Arial", Font.PLAIN, 14));
optionGroup.add(optionButtons[i]);
quizPanel.add(optionButtons[i]);
}
submitButton = new JButton("Submit");
quizPanel.add(submitButton);
frame.add(quizPanel, "Quiz");
frame.setVisible(true);
// Action listeners
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
login();
}
});
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
submitAnswer();
}
});
}
private void login() {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if ("user".equals(username) && "pass".equals(password)) {
CardLayout cl = (CardLayout) frame.getLayout();
cl.show(frame.getContentPane(), "Quiz");
loadQuestion();
} else {
JOptionPane.showMessageDialog(frame, "Invalid username or password.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void loadQuestion() {
if (currentQuestion < questions.length) {
String[] questionData = questions[currentQuestion];
questionLabel.setText(questionData[0]);
for (int i = 0; i < 4; i++) {
optionButtons[i].setText(questionData[i + 1]);
}
optionGroup.clearSelection();
} else {
showResult();
}
}
private void submitAnswer() {
String selectedOption = null;
for (JRadioButton optionButton : optionButtons) {
if (optionButton.isSelected()) {
selectedOption = optionButton.getText();
break;
}
}
if (selectedOption == null) {
JOptionPane.showMessageDialog(frame, "Please select an option.", "Warning", JOptionPane.WARNING_MESSAGE);
return;
}
String correctAnswer = questions[currentQuestion][5];
if (selectedOption.equals(correctAnswer)) {
score++;
}
currentQuestion++;
loadQuestion();
}
private void showResult() {
JOptionPane.showMessageDialog(frame, "You scored " + score + " out of " + questions.length, "Quiz Result", JOptionPane.INFORMATION_MESSAGE);
frame.dispose(); // Close the application after showing the result
}
}