<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          手把手教你用Java打造一款簡單考試系統(tǒng)(下篇)

          共 1665字,需瀏覽 4分鐘

           ·

          2021-02-06 00:20

          點擊上方“Java進階學(xué)習(xí)交流”,進行關(guān)注

          后臺回復(fù)“Java”即可獲贈Java學(xué)習(xí)資料

          上有六龍回日之高標,下有沖波逆折之回川。?

          上篇文章我們介紹了簡單考試系統(tǒng)的理論部分,這篇文章我們來一起看下其具體的實現(xiàn)。

          一、項目背景

          隨著移動互聯(lián)網(wǎng)的發(fā)展,網(wǎng)絡(luò)給我們帶來的是無窮的信息,也帶來了便利。與傳統(tǒng)考試模式相對比,在線考試具有很多優(yōu)越性、它可以將傳統(tǒng)考試過程中的試卷組織、傳送、收集、評判等各環(huán)節(jié)縮小到一至兩個環(huán)節(jié),不僅可以節(jié)約大量的時間、人力、物力與財力,還可以大幅度提高考試的客觀性和公正性。利用現(xiàn)有的計算機硬、軟件和網(wǎng)絡(luò)資源實現(xiàn)無紙質(zhì)考試以避免傳統(tǒng)考試的不足。


          二、項目目標

          設(shè)計一款應(yīng)用程序,顯示駕照考試科目一的題目,進行計時,當(dāng)用戶提交試卷后,判斷用戶的做題情況,統(tǒng)計得分,并顯示考試結(jié)果。


          三、項目實施

          首先回顧上一節(jié)的Java簡單考試系統(tǒng)(上篇),完成界面的窗口、題目和選項、顯示進度、顯示按鈕和時間、顯示總分和表情,效果如下圖所示。

          324d4d20354c04923158c4647ae13406.webp

          接下來,小編帶大家完成剩下的功能,具體的實現(xiàn)步驟如下。

          (一)顯示背景圖,完成界面設(shè)計

          1.setOpaque設(shè)置控件是否透明的,true表示不透明,false表示透明;

            buttona.setOpaque(false);  buttonb.setOpaque(false);  buttonc.setOpaque(false);  buttond.setOpaque(false);  panel01.setOpaque(false);  panel02.setOpaque(false);  panel03.setOpaque(false);  panel04.setOpaque(false);

          2.設(shè)置邊界

            label.setBounds(0, 0, bg.getIconWidth(), bg.getIconHeight());

          3.把整個窗格轉(zhuǎn)化為面板

           imagePanel=(JPanel)this.getContentPane(); imagePanel.setOpaque(false);

          4.把背景圖添加到分層窗格的最底層

          this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
          效果圖如下圖所示:

          80288157d4c00d29f9306a4196c4d0ef.webp

          (二)給控件屬性添加監(jiān)聽事件

          btn_last.addActionListener(ml);btn_next.addActionListener(ml);btn_finish.addActionListener(ml);buttona.addActionListener(ml);buttonb.addActionListener(ml);buttonc.addActionListener(ml);buttond.addActionListener(ml);

          (三)創(chuàng)建計時器,然后讓計時器啟動

            timer = new Timer(1000,new TimerListener());            timer.start();

          (四)完成事件處理

          1.紅色按鈕表示未做的題,綠色表示已做的題。

          public class MyListener implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {        // TODO Auto-generated method stub        for(int i=0;i<5;i++){            if(e.getSource()==btn_index[i]){//按鈕1到5                num = i;//更新當(dāng)前題號                showItem(num);//切換題目和選項                showMychoice(num);//顯示已選選項                showButton(num);//判斷顯示哪些按鈕            }        }

          (1)實現(xiàn)上一題功能

          if(e.getSource()==btn_last){            if(num>0){                num--;            }            showItem(num);            showMychoice(num);            showButton(num);        }

          (2)實現(xiàn)下一題功能

          if(e.getSource()==btn_next){            if(num-1){                num++;            }            showItem(num);            showMychoice(num);            showButton(num);        }

          (3)實現(xiàn)單選功能

          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); }

          (4)交卷,停止計時器

          if(e.getSource()==btn_finish){//交卷            timer.stop();//停止            TextFinish();
          }}}

          效果圖如下圖:

          629c38b00ed957d01d4a43c76d7e7932.webp

          2.用戶自己提交試卷,當(dāng)分數(shù)不是滿分則顯示大哭的表情。

          (1)設(shè)置選中的答案

          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();//清空}

          (2)顯示已選選項,i為當(dāng)前題號

          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;    }}

          (3)判斷顯示哪些按鈕,i為當(dāng)前題號

          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); } }

          (4)考試結(jié)束的方法

          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);}

          4.1判斷用戶選的答案是否正確

          for(int i=0;i<4;i++){        btn_index[i].setEnabled(false);        if(my_answer[i]==right[i]){            score=score+20;        }}

          4.2 設(shè)置分數(shù),如果分數(shù)等于100分笑臉,反之哭臉

          label_score.setText("總成績:"+score);if(score==100){        image.setIcon(new ImageIcon("image//lauge.jpg"));           }else{        image.setIcon(new ImageIcon("image//cry.jpg"));  }}

          效果圖如下圖:

          15fce2b861f6ae020aed85ea1519becd.webp

          3.考試時間到,系統(tǒng)自動提交試卷。當(dāng)分數(shù)為滿分則顯示大笑的表情。

          public class TimerListener implements ActionListener{
          @Overridepublic void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub second--; if(second<0){ minute--; second=59; } label_time.setText(minute+":"+second); if(minute==0 && second==0){ timer.stop(); label_time.setText("考試結(jié)束!"); TextFinish(); } }
          }

          效果圖如下圖:

          e89ac747ae3d2af353db2286cd293b9e.webp

          四、總結(jié)

          1.介紹了JLabel、JButton、JPanel、ButtonGroup、JRadioButton單選框組件的基本使用,以及相應(yīng)的事件處理,完成界面的設(shè)計。

          2.事件處理函數(shù)的添加,難點是運用理解構(gòu)造函數(shù)、內(nèi)部類的創(chuàng)建。這些代碼比較簡單,也是一個簡單的小案例,希望對你有所幫助!

          3.如果有需要本文項目代碼的小伙伴,可以在后臺回復(fù)“考試系統(tǒng)”四個字進行獲取。

          -------------------?End?-------------------

          往期精彩文章推薦:

          26d59f384df646bf44f0c0d33c5fe6f9.webp

          歡迎大家點贊,留言,轉(zhuǎn)發(fā),轉(zhuǎn)載,感謝大家的相伴與支持

          想加入Python學(xué)習(xí)群請在后臺回復(fù)【入群

          萬水千山總是情,點個【在看】行不行

          瀏覽 74
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  豆花视频国产一区 | 免费观看一级二级网站 | 免费日韩黄色电影 | 大香蕉国产毛片儿操逼申影 | 亚洲熟妇性ⅩXXX交潮喷 |