手把手教你用Java打造一款簡單考試系統(tǒng)之一
一、項目背景
隨著移動互聯(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è)計一款應用程序,顯示駕照考試科目一的題目,進行計時,當用戶提交試卷后,判斷用戶的做題情況,統(tǒng)計得分,并顯示考試結(jié)果。
三、項目實施
使用eclipse軟件開發(fā),先上效果圖,如下圖所示。可以看到在界面上有可以有駕照考試科目一的題目,考試的時間,提交試卷,頁面切換的功能。
接下來,小編帶大家進行具體的實現(xiàn),具體的實現(xiàn)步驟如下。
(一)首先實現(xiàn)窗體界面
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- testsystem t = new testsystem();
- t.setTitle("駕照考試");
- t.setSize(660,430);
- t.setVisible(true);
- t.setResizable(false);//設(shè)置窗口是否可以調(diào)整
- t.setLocationRelativeTo(null);//null表示沒有參照物,居中電腦
- }
使用new關(guān)鍵字創(chuàng)建testsystem類:
- setTitle表示設(shè)置界面的標題;
- setSize(寬,高)表示窗體大?。?nbsp;
- setVisible(true或false)表示窗體是否可見;
- setResizable(true或false)表示窗體是否可以由用戶調(diào)整大?。?nbsp;
- setLocationRelativeTo()表示設(shè)置窗口相對于指定組件的位置。
效果圖如下圖:
(二)界面的設(shè)計
1.顯示的界面:創(chuàng)建JFrame實例、JPanel面板,然后把面板添加到JFrame中;
2.構(gòu)造一個按鈕組對象ButtonGroup,把JRadioButton類型的對象添加到該按鈕組中;
- 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、在實習期內(nèi)駕駛機動車的,應當在車身后部粘貼或者懸掛哪種標志?",
- "2、初次申領(lǐng)的機動車駕駛證的有效期為多少年?",
- "3、夜間道路環(huán)境對安全行車的主要影響是什么?",
- "4、路中心雙黃實線是何含義?",
- "5、駕駛車輛行至道路急轉(zhuǎn)彎處,應怎樣做?"
- };
- //ABCD選項
- private String answer_a[]=new String[]{
- "A、注意新手標志",
- "A、3年",
- "A、能見度低、不利于觀察道路交通情況",
- "A、可跨越對向車道分界線",
- "A、借對向車道行駛"
- };
- private String answer_b[]=new String[]{
- "B、注意避讓標志",
- "B、5年",
- "B、路面復雜多變",
- "B、禁止跨越對向車行道分界線",
- "B、急劇制動低速通過"
- };
- private String answer_c[]=new String[]{
- "C、統(tǒng)一式樣的實習標志",
- "C、6年",
- "C、駕駛?cè)梭w力下降",
- "C、雙側(cè)可跨越同向車道分界線",
- "C、靠彎道外側(cè)行駛"
- };
- private String answer_d[]=new String[]{
- "D、注意車距標注",
- "D、12年",
- "D、駕駛?cè)艘桩a(chǎn)生沖動、幻覺",
- "D、單向行駛車道分界線",
- "D、充分減速并靠右側(cè)行駛"
- };
- private int num=0;//當前題號
3.切換題目,交卷按鈕,顯示時間;
- 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("剩下時間");
- private JLabel label_time=new JLabel("5:00");
4.換壁紙,顯示分數(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;//當前分數(shù)為0
6.創(chuàng)建計時器。
- 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類型的對象添加到該按鈕組中實現(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.添加題目和選項;
- 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.五個選題的按鈕;
- 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.添加上一題、下一題、交卷、時間;
- btn_last.setEnabled(false);//設(shè)置最后一題的不能再點擊下一題
- 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.添加顯示分數(shù)
注意:調(diào)試完先不顯示總分和表情,等用戶交卷后再顯示相應總分和表情。
- 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è)計先到這里,接下實現(xiàn)功能的請看java簡單考試系統(tǒng)(下篇)!
四、總結(jié)
1.本文主要介紹了JLabel、JButton、JPanel、ButtonGroup、JRadioButton單選框組件的基本使用,完成界面的窗口、題目和選項、顯示進度、顯示按鈕和時間、顯示總分和表情。
2.這些代碼比較簡單,也是一個簡單的小案例,希望對你有所幫助。針對功能的實現(xiàn)請看java簡單考試系統(tǒng)(下篇)。
本文轉(zhuǎn)載自微信公眾號「Java進階學習交流」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系Java進階學習交流公眾號。