Build a Java Exam App: Step‑by‑Step Swing Implementation
This article walks through creating a Java Swing‑based exam application that displays driving‑license questions, handles timing, records answers, calculates scores, and shows result icons, providing complete code snippets and UI design guidance.
Project Background
With the rise of mobile internet, online exams offer many advantages over traditional paper tests, such as reduced time, labor, and cost, as well as improved objectivity and fairness. Leveraging existing computer hardware, software, and network resources enables paper‑less examinations.
Project Goals
Design an application that displays driving‑license theory questions, includes a timer, evaluates user answers upon submission, calculates the score, and presents the exam result.
Implementation
First, review the previous simple Java exam system (part one) to complete the window, questions, options, progress display, buttons, timer, total score, and emoticon display. The initial UI looks like the image below.
Next, complete the remaining features as follows.
(1) Show background image and design the UI
Set component opacity:
buttona.setOpaque(false);
buttonb.setOpaque(false);
buttonc.setOpaque(false);
buttond.setOpaque(false);
panel01.setOpaque(false);
panel02.setOpaque(false);
panel03.setOpaque(false);
panel04.setOpaque(false);Set bounds for the background label:
label.setBounds(0, 0, bg.getIconWidth(), bg.getIconHeight());Convert the content pane to a panel:
imagePanel = (JPanel) this.getContentPane();
imagePanel.setOpaque(false);Add the background label to the lowest layer of the layered pane:
this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));(2) Add listeners to components
btn_last.addActionListener(ml);
btn_next.addActionListener(ml);
btn_finish.addActionListener(ml);
buttona.addActionListener(ml);
buttonb.addActionListener(ml);
buttonc.addActionListener(ml);
buttond.addActionListener(ml);(3) Create and start the timer
timer = new Timer(1000, new TimerListener());
timer.start();(4) Event handling
Red button = unanswered, green button = answered.
public class MyListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 5; i++) {
if (e.getSource() == btn_index[i]) { // buttons 1‑5
num = i; // update current question index
showItem(num);
showMychoice(num);
showButton(num);
}
}
}
}Previous question:
if (e.getSource() == btn_last) {
if (num > 0) {
num--;
}
showItem(num);
showMychoice(num);
showButton(num);
}Next question:
if (e.getSource() == btn_next) {
if (num < str_problem.length - 1) {
num++;
}
showItem(num);
showMychoice(num);
showButton(num);
}Single‑choice handling:
if (e.getSource() == buttona) { my_answer[num] = 1; btn_index[num].setBackground(Color.GREEN); }
if (e.getSource() == buttonb) { my_answer[num] = 2; btn_index[num].setBackground(Color.GREEN); }
if (e.getSource() == buttonc) { my_answer[num] = 3; btn_index[num].setBackground(Color.GREEN); }
if (e.getSource() == buttond) { my_answer[num] = 4; btn_index[num].setBackground(Color.GREEN); }Submit exam and stop timer:
if (e.getSource() == btn_finish) {
timer.stop();
TextFinish();
}(5) Helper methods
public void showItem(int i) {
problem.setText(str_problem[i]);
buttona.setText(answer_a[i]);
buttonb.setText(answer_b[i]);
buttonc.setText(answer_c[i]);
buttond.setText(answer_d[i]);
group.clearSelection();
}
public void showMychoice(int i) {
switch (my_answer[i]) {
case 1: buttona.setSelected(true); break;
case 2: buttonb.setSelected(true); break;
case 3: buttonc.setSelected(true); break;
case 4: buttond.setSelected(true); break;
}
}
public void showButton(int i) {
if (i == 0) {
btn_last.setEnabled(false);
btn_next.setEnabled(true);
} else if (i == str_problem.length - 1) {
btn_last.setEnabled(true);
btn_next.setEnabled(false);
} else {
btn_last.setEnabled(true);
btn_next.setEnabled(true);
}
}
public void TextFinish() {
btn_last.setEnabled(false);
btn_next.setEnabled(false);
btn_finish.setEnabled(false);
buttona.setEnabled(false);
buttonb.setEnabled(false);
buttonc.setEnabled(false);
buttond.setEnabled(false);
}
for (int i = 0; i < 4; i++) {
btn_index[i].setEnabled(false);
if (my_answer[i] == right[i]) {
score = score + 20;
}
}
label_score.setText("Total Score:" + score);
if (score == 100) {
image.setIcon(new ImageIcon("image//lauge.jpg"));
} else {
image.setIcon(new ImageIcon("image//cry.jpg"));
}(6) Timer for automatic submission
public class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
second--;
if (second < 0) {
minute--;
second = 59;
}
label_time.setText(minute + ":" + second);
if (minute == 0 && second == 0) {
timer.stop();
label_time.setText("Exam Over!");
TextFinish();
}
}
}Conclusion
1. Demonstrated basic usage of JLabel, JButton, JPanel, ButtonGroup, and JRadioButton components and their event handling to build the UI.
2. Added event‑handling functions; the main difficulty lies in understanding constructors and inner‑class creation. The code is straightforward and serves as a simple example.
3. To obtain the full project code, reply with the keyword “考试系统” in the backend.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
