手把手教你用Java打造一款簡(jiǎn)單考試系統(tǒng)(上篇)
后臺(tái)回復(fù)“Java”即可獲贈(zèng)Java學(xué)習(xí)資料
一、項(xiàng)目背景
隨著移動(dòng)互聯(lián)網(wǎng)的發(fā)展,網(wǎng)絡(luò)給我們帶來(lái)的是無(wú)窮的信息,也帶來(lái)了便利。與傳統(tǒng)考試模式相對(duì)比,在線考試具有很多優(yōu)越性、它可以將傳統(tǒng)考試過(guò)程中的試卷組織、傳送、收集、評(píng)判等各環(huán)節(jié)縮小到一至兩個(gè)環(huán)節(jié),不僅可以節(jié)約大量的時(shí)間、人力、物力與財(cái)力,還可以大幅度提高考試的客觀性和公正性。利用現(xiàn)有的計(jì)算機(jī)硬、軟件和網(wǎng)絡(luò)資源實(shí)現(xiàn)無(wú)紙質(zhì)考試以避免傳統(tǒng)考試的不足。
二、項(xiàng)目目標(biāo)
設(shè)計(jì)一款應(yīng)用程序,顯示駕照考試科目一的題目,進(jìn)行計(jì)時(shí),當(dāng)用戶提交試卷后,判斷用戶的做題情況,統(tǒng)計(jì)得分,并顯示考試結(jié)果。
三、項(xiàng)目實(shí)施
使用eclipse軟件開(kāi)發(fā),先上效果圖,如下圖所示??梢钥吹皆诮缑嫔嫌锌梢杂旭{照考試科目一的題目,考試的時(shí)間,提交試卷,頁(yè)面切換的功能。

接下來(lái),小編帶大家進(jìn)行具體的實(shí)現(xiàn),具體的實(shí)現(xiàn)步驟如下。
(一)首先實(shí)現(xiàn)窗體界面
public static void main(String[] args) {// TODO Auto-generated method stubtestsystem t = new testsystem();t.setTitle("駕照考試");t.setSize(660,430);t.setVisible(true);t.setResizable(false);//設(shè)置窗口是否可以調(diào)整t.setLocationRelativeTo(null);//null表示沒(méi)有參照物,居中電腦}
使用new關(guān)鍵字創(chuàng)建testsystem類:
setTitle表示設(shè)置界面的標(biāo)題;setSize(寬,高)表示窗體大??;setVisible(true或false)表示窗體是否可見(jiàn);setResizable(true或false)表示窗體是否可以由用戶調(diào)整大?。?/span>setLocationRelativeTo()表示設(shè)置窗口相對(duì)于指定組件的位置。
效果圖如下圖:

(二)界面的設(shè)計(jì)
1.顯示的界面:創(chuàng)建JFrame實(shí)例、JPanel面板,然后把面板添加到JFrame中;
2.構(gòu)造一個(gè)按鈕組對(duì)象ButtonGroup,把JRadioButton類型的對(duì)象添加到該按鈕組中;
public class testsystem extends JFrame{//變量private JPanel panel01 =new JPanel();private JLabel problem =new JLabel();private ButtonGroup group=new ButtonGroup();private JRadioButton buttona=new JRadioButton();private JRadioButton buttonb=new JRadioButton();private JRadioButton buttonc=new JRadioButton();private JRadioButton buttond=new JRadioButton();private String str_problem[]=new String[]{"1、在實(shí)習(xí)期內(nèi)駕駛機(jī)動(dòng)車的,應(yīng)當(dāng)在車身后部粘貼或者懸掛哪種標(biāo)志?","2、初次申領(lǐng)的機(jī)動(dòng)車駕駛證的有效期為多少年?","3、夜間道路環(huán)境對(duì)安全行車的主要影響是什么?","4、路中心雙黃實(shí)線是何含義?","5、駕駛車輛行至道路急轉(zhuǎn)彎處,應(yīng)怎樣做?"};//ABCD選項(xiàng)private String answer_a[]=new String[]{"A、注意新手標(biāo)志","A、3年","A、能見(jiàn)度低、不利于觀察道路交通情況","A、可跨越對(duì)向車道分界線","A、借對(duì)向車道行駛"};private String answer_b[]=new String[]{"B、注意避讓標(biāo)志","B、5年","B、路面復(fù)雜多變","B、禁止跨越對(duì)向車行道分界線","B、急劇制動(dòng)低速通過(guò)"};private String answer_c[]=new String[]{"C、統(tǒng)一式樣的實(shí)習(xí)標(biāo)志","C、6年","C、駕駛?cè)梭w力下降","C、雙側(cè)可跨越同向車道分界線","C、靠彎道外側(cè)行駛"};private String answer_d[]=new String[]{"D、注意車距標(biāo)注","D、12年","D、駕駛?cè)艘桩a(chǎn)生沖動(dòng)、幻覺(jué)","D、單向行駛車道分界線","D、充分減速并靠右側(cè)行駛"};private int num=0;//當(dāng)前題號(hào)
3.切換題目,交卷按鈕,顯示時(shí)間;
private JPanel panel02=new JPanel();private JButton btn_index[]=new JButton[5];private JPanel panel03=new JPanel();private JButton btn_last=new JButton("上一題");private JButton btn_next=new JButton("下一題");private JButton btn_finish=new JButton("交卷");private JLabel label01=new JLabel("剩下時(shí)間");private JLabel label_time=new JLabel("5:00");
4.換壁紙,顯示分?jǐn)?shù);
private JPanel panel04 = new JPanel();private JLabel label_score = new JLabel();private JLabel image = new JLabel(new ImageIcon());private JPanel imagePanel;private ImageIcon bg = new ImageIcon("image//bg.jpg");private JLabel label = new JLabel(bg);private MyListener ml = new MyListener();
5.判斷選的答案是否正確;
private int right[] = new int[]{3,3,1,2,4};//正確答案private int my_answer[]=new int[]{0,0,0,0,0};//用戶答案private int score = 0;//當(dāng)前分?jǐn)?shù)為0
6.創(chuàng)建計(jì)時(shí)器。
private Timer timer;private int minute=4,second=60;
(三)在testsystem類的構(gòu)造函數(shù)設(shè)置組件的屬性
1.設(shè)置題目,ABCD字體;
problem.setFont(new Font("宋體",Font.BOLD,18));buttona.setFont(new Font("宋體",Font.BOLD,18));buttonb.setFont(new Font("宋體",Font.BOLD,18));buttonc.setFont(new Font("宋體",Font.BOLD,18));buttond.setFont(new Font("宋體",Font.BOLD,18));problem.setText(str_problem[num]);buttona.setText(answer_a[num]);buttonb.setText(answer_b[num]);buttonc.setText(answer_c[num]);buttond.setText(answer_d[num]);
2.把JRadioButton類型的對(duì)象添加到該按鈕組中實(shí)現(xiàn)單選功能;
group.add(buttona);group.add(buttonb);group.add(buttonc);group.add(buttond);
3.GridLayout網(wǎng)格布局:行,列,水平間距,垂直間距;
panel01.setLayout(new GridLayout(5, 1, 0, 30));4.添加題目和選項(xiàng);
panel01.add(problem);panel01.add(buttona);panel01.add(buttonb);panel01.add(buttonc);panel01.add(buttond);this.setLayout(new BorderLayout());this.add(panel01,BorderLayout.NORTH);
效果圖如下圖:

5.五個(gè)選題的按鈕;
for(int i=0;i<5;i++){btn_index[i]=new JButton(""+(i+1));btn_index[i].setBackground(Color.red);panel02.add(btn_index[i]);btn_index[i].addActionListener(ml);}this.add(panel02,BorderLayout.CENTER);
效果圖如下圖:

6.添加上一題、下一題、交卷、時(shí)間;
btn_last.setEnabled(false);//設(shè)置最后一題的不能再點(diǎn)擊下一題label_time.setFont(new Font("黑體",Font.BOLD,30));label_time.setForeground(Color.RED);panel03.add(btn_last);panel03.add(btn_next);panel03.add(btn_finish);panel03.add(label01);panel03.add(label_time);this.add(panel03,BorderLayout.SOUTH);
效果圖如下圖:

7.添加顯示分?jǐn)?shù)
注意:調(diào)試完先不顯示總分和表情,等用戶交卷后再顯示相應(yīng)總分和表情。
label_score.setFont(new Font("黑體",Font.PLAIN,30));label_score.setForeground(Color.BLUE);panel04.add(label_score);panel04.add(image);this.add(panel04,BorderLayout.EAST);
效果圖如下圖:

小編寫的界面設(shè)計(jì)先到這里,接下實(shí)現(xiàn)功能的請(qǐng)看java簡(jiǎn)單考試系統(tǒng)(下篇)!
四、總結(jié)
1.本文主要介紹了JLabel、JButton、JPanel、ButtonGroup、JRadioButton單選框組件的基本使用,完成界面的窗口、題目和選項(xiàng)、顯示進(jìn)度、顯示按鈕和時(shí)間、顯示總分和表情。
2.這些代碼比較簡(jiǎn)單,也是一個(gè)簡(jiǎn)單的小案例,希望對(duì)你有所幫助。針對(duì)功能的實(shí)現(xiàn)請(qǐng)看java簡(jiǎn)單考試系統(tǒng)(下篇)。
-------------------?End?-------------------
往期精彩文章推薦:

歡迎大家點(diǎn)贊,留言,轉(zhuǎn)發(fā),轉(zhuǎn)載,感謝大家的相伴與支持
想加入Python學(xué)習(xí)群請(qǐng)?jiān)诤笈_(tái)回復(fù)【入群】
萬(wàn)水千山總是情,點(diǎn)個(gè)【在看】行不行
