Fundamentals 12 min read

Build a Java Swing Exam App: Step‑by‑Step GUI Tutorial

This article walks through building a Java Swing‑based driver’s license exam application, covering project background, objectives, UI design, core code snippets, component layout, timer and scoring logic, and provides screenshots of each development stage.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Build a Java Swing Exam App: Step‑by‑Step GUI Tutorial

1. Project Background

With the development of mobile internet, online examinations offer many advantages over traditional paper‑based tests, such as reducing time, labor, material costs, and improving objectivity and fairness. This project aims to implement a paper‑less exam system using existing computer hardware and network resources.

2. Project Goals

Design an application that displays questions for the first subject of the driver’s license exam, includes a timer, evaluates the user’s answers after submission, calculates the score, and shows the exam results.

3. Project Implementation

The development is done with Eclipse. The UI shows exam questions, timing, submission, and page‑switching functions.

Initial UI mockup
Initial UI mockup

(1) Create the Main Window

public static void main(String[] args) {<br/>    // TODO Auto-generated method stub<br/>    testsystem t = new testsystem();<br/>    t.setTitle("驾照考试");<br/>    t.setSize(660,430);<br/>    t.setVisible(true);<br/>    t.setResizable(false); // set window resizable<br/>    t.setLocationRelativeTo(null); // center on screen<br/>}

Explanation of key methods:

setTitle: set window title;<br/>setSize(width,height): set window size;<br/>setVisible(true/false): show or hide window;<br/>setResizable(true/false): allow user to resize;<br/>setLocationRelativeTo(): position window relative to a component.

(2) UI Design

1. Create a JFrame and add a JPanel to it. 2. Use a ButtonGroup to group JRadioButton objects for single‑choice selection.

public class testsystem extends JFrame{ // variables<br/>    private JPanel panel01 = new JPanel();<br/>    private JLabel problem = new JLabel();<br/>    private ButtonGroup group = new ButtonGroup();<br/>    private JRadioButton buttona = new JRadioButton();<br/>    private JRadioButton buttonb = new JRadioButton();<br/>    private JRadioButton buttonc = new JRadioButton();<br/>    private JRadioButton buttond = new JRadioButton();<br/>    private String[] str_problem = {<br/>        "1、在实习期内驾驶机动车的,应当在车身后部粘贴或者悬挂哪种标志?",<br/>        "2、初次申领的机动车驾驶证的有效期为多少年?",<br/>        "3、夜间道路环境对安全行车的主要影响是什么?",<br/>        "4、路中心双黄实线是何含义?",<br/>        "5、驾驶车辆行至道路急转弯处,应怎样做?"<br/>    };<br/>    private String[] answer_a = {"A、注意新手标志","A、3年","A、能见度低、不利于观察道路交通情况","A、可跨越对向车道分界线","A、借对向车道行驶"};<br/>    private String[] answer_b = {"B、注意避让标志","B、5年","B、路面复杂多变","B、禁止跨越对向车行道分界线","B、急剧制动低速通过"};<br/>    private String[] answer_c = {"C、统一式样的实习标志","C、6年","C、驾驶人体力下降","C、双侧可跨越同向车道分界线","C、靠弯道外侧行驶"};<br/>    private String[] answer_d = {"D、注意车距标注","D、12年","D、驾驶人易产生冲动、幻觉","D、单向行驶车道分界线","D、充分减速并靠右侧行驶"};<br/>    private int num = 0; // current question index

3. Switch questions, submit button, and display timer.

private JPanel panel02 = new JPanel();<br/>private JButton[] btn_index = new JButton[5];<br/>private JPanel panel03 = new JPanel();<br/>private JButton btn_last = new JButton("上一题");<br/>private JButton btn_next = new JButton("下一题");<br/>private JButton btn_finish = new JButton("交卷");<br/>private JLabel label01 = new JLabel("剩下时间");<br/>private JLabel label_time = new JLabel("5:00");

4. Change background and display score.

private JPanel panel04 = new JPanel();<br/>private JLabel label_score = new JLabel();<br/>private JLabel image = new JLabel(new ImageIcon());<br/>private ImageIcon bg = new ImageIcon("image//bg.jpg");<br/>private JLabel label = new JLabel(bg);<br/>private MyListener ml = new MyListener();

5. Store correct answers and user selections.

private int[] right = {3,3,1,2,4}; // correct options<br/>private int[] my_answer = {0,0,0,0,0}; // user answers<br/>private int score = 0; // current score

6. Timer for the exam.

private Timer timer;<br/>private int minute = 4, second = 60;

(3) Set Component Properties in Constructor

problem.setFont(new Font("宋体", Font.BOLD, 18));<br/>buttona.setFont(new Font("宋体", Font.BOLD, 18));<br/>buttonb.setFont(new Font("宋体", Font.BOLD, 18));<br/>buttonc.setFont(new Font("宋体", Font.BOLD, 18));<br/>buttond.setFont(new Font("宋体", Font.BOLD, 18));<br/>problem.setText(str_problem[num]);<br/>buttona.setText(answer_a[num]);<br/>buttonb.setText(answer_b[num]);<br/>buttonc.setText(answer_c[num]);<br/>buttond.setText(answer_d[num]);

Add radio buttons to the group for single‑choice behavior:

group.add(buttona);<br/>group.add(buttonb);<br/>group.add(buttonc);<br/>group.add(buttond);

Use GridLayout for the question panel: panel01.setLayout(new GridLayout(5,1,0,30)); Add components to panels and set the main layout:

panel01.add(problem);<br/>panel01.add(buttona);<br/>panel01.add(buttonb);<br/>panel01.add(buttonc);<br/>panel01.add(buttond);<br/>this.setLayout(new BorderLayout());<br/>this.add(panel01, BorderLayout.NORTH);
Question panel UI
Question panel UI

Create navigation buttons for each question:

for(int i=0;i<5;i++){<br/>    btn_index[i]=new JButton(""+(i+1));<br/>    btn_index[i].setBackground(Color.red);<br/>    panel02.add(btn_index[i]);<br/>    btn_index[i].addActionListener(ml);<br/>}<br/>this.add(panel02, BorderLayout.CENTER);
Navigation buttons UI
Navigation buttons UI

Add previous/next/submit buttons and timer label:

btn_last.setEnabled(false); // disable previous on first question<br/>label_time.setFont(new Font("黑体", Font.BOLD, 30));<br/>label_time.setForeground(Color.RED);<br/>panel03.add(btn_last);<br/>panel03.add(btn_next);<br/>panel03.add(btn_finish);<br/>panel03.add(label01);<br/>panel03.add(label_time);<br/>this.add(panel03, BorderLayout.SOUTH);
Control panel UI
Control panel UI

Display the final score after submission:

label_score.setFont(new Font("黑体", Font.PLAIN, 30));<br/>label_score.setForeground(Color.BLUE);<br/>panel04.add(label_score);<br/>panel04.add(image);<br/>this.add(panel04, BorderLayout.EAST);
Score display UI
Score display UI

4. Summary

This article introduced the basic use of JLabel, JButton, JPanel, ButtonGroup, and JRadioButton components, creating a window, displaying questions and options, showing progress, handling navigation, timer, and final scoring. The code is simple and serves as a small case study for Java GUI development; see the next part for functional implementation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaGUIDesktopSwingexam
Python Crawling & Data Mining
Written by

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!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.