最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java swing讀取txt文件實(shí)現(xiàn)學(xué)生考試系統(tǒng)

 更新時(shí)間:2020年06月10日 15:22:04   作者:Woo_home  
這篇文章主要為大家詳細(xì)介紹了Java swing讀取txt文件實(shí)現(xiàn)學(xué)生考試系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java swing讀取txt文件實(shí)現(xiàn)學(xué)生考試系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

主要實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的倒計(jì)時(shí)答題系統(tǒng)

源碼Testquestion 類(lèi)

public class Testquestion {
 private String questionText ="";//定義題目
 private String standardkey = "";// 定義正確答案
 private String selectKey ="";// 定義輸入答案
 public Testquestion(String questionText, String standardkey) {
 super();
 this.questionText = questionText;
 this.standardkey = standardkey;
 }
 public String getQuestionText() {
 return questionText;
 }
 public void setQuestionText(String questionText) {
 this.questionText = questionText;
 }
 public String getStandardkey() {
 return standardkey;
 }
 public void setStandardkey(String standardkey) {
 this.standardkey = standardkey;
 }
 public String getSelectKey() {
 return selectKey;
 }
 public void setSelectKey(String selectKey) {
 this.selectKey = selectKey;
 }
 public boolean check() {
 if (this.selectKey.equals(this.standardkey)) {
 return true;
 }
 else {
 return false;
 }
 } 
 }

主程序Test2

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.NumberFormat;
import java.util.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class Test2 extends JFrame implements ActionListener{
 
 private JButton start,commit,back,next;
 private JRadioButton aButton,bButton,cButton,dButton;
 private ButtonGroup buttonGroup;
 private JLabel label,clock;
 private JTextArea jTextArea;
 private JPanel panel,panel2,panel3;
 Testquestion t1;
 Testquestion[] questions;
 int examtime;
 int p=0;//設(shè)置題目數(shù)指針  
 int topicnum=0;
 int right,error;       //答對(duì)和答錯(cuò)
 ClockDispaly mt;       //倒計(jì)時(shí)模塊
 
 public Test2(){
 
 this.setTitle("學(xué)生在線(xiàn)考試系統(tǒng)v1");     //設(shè)置標(biāo)題
 this.setSize(440,320);      //設(shè)置窗口大小
 this.setLocationRelativeTo(null);    //設(shè)置顯示位置居中
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //設(shè)置關(guān)閉時(shí)關(guān)閉
 
 panel = new JPanel();      //初始化面板
 panel2 = new JPanel();
 panel3 = new JPanel();
 label = new JLabel("總考試時(shí)間:100分鐘 ");    //初始化并命名標(biāo)簽
 clock = new JLabel();
 jTextArea = new JTextArea(10,35);    //初始化文本區(qū)域
 jTextArea.setEditable(false);     //設(shè)置文本不可修改
 
 aButton = new JRadioButton("A");    //初始化單選按鈕
 bButton = new JRadioButton("B");
 cButton = new JRadioButton("C");
 dButton = new JRadioButton("D");
 buttonGroup = new ButtonGroup();     //初始化選項(xiàng)組
 
 start = new JButton("開(kāi)始考試");     //初始化按鍵
 back = new JButton("上一題");
 next = new JButton("下一題");
 commit = new JButton("提交考試");
 
 aButton.addActionListener(this);    //單選按鈕添加監(jiān)聽(tīng)事件
 bButton.addActionListener(this);
 cButton.addActionListener(this);
 dButton.addActionListener(this);
 
 start.addActionListener(this);    //按鈕添加監(jiān)聽(tīng)事件
 back.addActionListener(this);
 next.addActionListener(this);
 commit.addActionListener(this);
 
 
 buttonGroup.add(aButton);     //把單選按鈕放到選項(xiàng)組
 buttonGroup.add(bButton);
 buttonGroup.add(cButton);
 buttonGroup.add(dButton);
 
 panel.add(label);      //把標(biāo)簽放入面板panel
 panel.add(clock);
 panel.add(start);      //把按鍵放入面板panel
 panel2.add(jTextArea);     //把文本區(qū)域放入面板panel2
 panel3.add(aButton);      //把單選按鈕放入面板panel3
 panel3.add(bButton);
 panel3.add(cButton);
 panel3.add(dButton);
 panel3.add(back);      //把按鍵放入面板panel3
 panel3.add(next);
 panel3.add(commit); 
 
 this.add(panel,BorderLayout.NORTH);    //設(shè)置面板panel放在上面
 this.add(panel2,BorderLayout.CENTER);    //設(shè)置面板panel2放在中間
 this.add(panel3, BorderLayout.SOUTH);    //設(shè)置面板panel放在下面
 
 this.setVisible(true);     //設(shè)置窗口可見(jiàn)
 
 mt = new ClockDispaly(clock, 30);    //調(diào)用并設(shè)置倒計(jì)時(shí)的時(shí)間
 }
 
 public void createExam() {//創(chuàng)建考試模塊
 Vector<Testquestion> qList=null;//創(chuàng)建一個(gè)向量列表,用于動(dòng)態(tài)增加試題
 Testquestion t;
 String questionText="";
 String standardKey;
 String s;
 try {
 FileReader fr=new FileReader("D:\\test.txt"); 
 BufferedReader br = new BufferedReader(fr); //可以每次讀一行 
 qList=new Vector<Testquestion>();
 while((s=br.readLine())!=null){//讀取試題
 if (s.equals("*****")){
  questionText="";//準(zhǔn)備接收一個(gè)題目的內(nèi)容
  s = br.readLine();//獲取試題內(nèi)容的首行
  
 }
 if (s.equals("$$$$$")){//準(zhǔn)備讀取試題的答案
  s = br.readLine(); //獲取試題的答案
  standardKey = s; //把試題答案賦值給正確答案 
  t = new Testquestion(questionText,standardKey); //把試題和答案賦值給t
  qList.add(t);     //把試題和答案賦值給列表
 }
 questionText=questionText+s+'\n';
 
 }
 br.close();//關(guān)閉緩沖流
 fr.close();//關(guān)閉文件流
 
 } 
 catch (IOException e) { 
 e.printStackTrace(); //打印異常信息
 }
 topicnum=qList.size();  //統(tǒng)計(jì)試題數(shù)量
 questions=new Testquestion[topicnum];
 for (int i=0;i<qList.size();i++) //讀取試題
 questions[i]=qList.get(i);
 
 }
 
 public void setSelected(String s) {//設(shè)置單選按鈕不重復(fù)模塊
 if (s.equals("A")) buttonGroup.setSelected(aButton.getModel(), true);
 if (s.equals("B")) buttonGroup.setSelected(bButton.getModel(), true);
 if (s.equals("C")) buttonGroup.setSelected(cButton.getModel(), true);
 if (s.equals("D")) buttonGroup.setSelected(dButton.getModel(), true);
 if (s.equals("")) buttonGroup.clearSelection();
 }
 
 public void showQuestion() {//設(shè)置試題模塊
 jTextArea.setText("");
 jTextArea.append(questions[p].getQuestionText());//在文本區(qū)域顯示試題
 setSelected(questions[p].getSelectKey());
 }
 
 public void showScore() {//設(shè)置成績(jī)模塊
 right=0;error=0;
 for (int i = 0; i < topicnum; i++) {
 if (questions[i].check()) {//判斷答案的正確與錯(cuò)誤
 right++;
 }else {
 error++;
 }
 }
 int score = (int)(right*100/topicnum);  //設(shè)置分?jǐn)?shù)
 JOptionPane.showMessageDialog(null, "答對(duì)"+right+"題,答錯(cuò)"+error+"題,分?jǐn)?shù)為"+score);
 }
 

 @Override
 public void actionPerformed(ActionEvent e) {//動(dòng)作監(jiān)聽(tīng)事件
 
 if (e.getSource()==start) {//開(kāi)始開(kāi)始按鍵實(shí)現(xiàn)
 createExam();  //調(diào)用createExam模塊
 p=0;   //題目序號(hào)
 showQuestion(); //調(diào)用showQuestion模塊
 start.setEnabled(false);//設(shè)置按鈕不可點(diǎn)擊
 mt.start();  //考試時(shí)間倒計(jì)時(shí)啟動(dòng)
 }
 if (e.getSource()==back) {//上一題的按鍵實(shí)現(xiàn)
 p--;
 if (p==-1) {
 JOptionPane.showMessageDialog(null, "已經(jīng)是第一題");
 p++;
 }
 showQuestion();
 }
 if (e.getSource()==next) {//下一題的按鍵實(shí)現(xiàn)
 p++;
 if (p==topicnum) {
 JOptionPane.showMessageDialog(null, "已經(jīng)是最后一題");
 p--;
 }
 showQuestion();
 }
 if (e.getSource()==commit) {//提交試卷的按鍵實(shí)現(xiàn)
 showScore();
 commit.setEnabled(false);
 System.exit(0);  //退出
 }
 
 if(e.getSource()==aButton) questions[p].setSelectKey("A");
 if(e.getSource()==bButton) questions[p].setSelectKey("B");
 if(e.getSource()==cButton) questions[p].setSelectKey("C");
 if(e.getSource()==dButton) questions[p].setSelectKey("D");
 
 }
 
 public static void main(String[] args) {
 new Test2();
 }
}

class ClockDispaly extends Thread{//設(shè)置Thread考試倒計(jì)時(shí)模塊
 
 private JLabel lefttimer;
 private int testtime;
 
 public ClockDispaly(JLabel lt,int time) {
 lefttimer = lt;
 testtime = time * 60;
 }
 public void run(){
 NumberFormat numberFormat = NumberFormat.getInstance();//控制時(shí)間的顯示格式
 numberFormat.setMinimumIntegerDigits(2);//設(shè)置數(shù)值的整數(shù)部分允許的最小位數(shù)
 int h,m,s;//定義時(shí)分秒
 while (testtime >= 0) {
 h = testtime / 3600;
 m = testtime % 3600 / 60;
 s = testtime % 60;
 StringBuffer stringBuffer = new StringBuffer("");
 //增加到lefttimer標(biāo)簽
 stringBuffer.append("考試剩余時(shí)間為:"+numberFormat.format(h)+":"+numberFormat.format(m)+":"+numberFormat.format(s));
 lefttimer.setText(stringBuffer.toString());
 try {
 Thread.sleep(1000);//延時(shí)一秒
 } catch (Exception e) {
 //ignore error
 }
 testtime = testtime - 1; 
 }
 if (testtime <= 0) {
 JOptionPane.showMessageDialog(null, "考試結(jié)束");
 System.exit(0);
 }
 }
}

txt文件

效果圖

正在嘗試寫(xiě)博客,如寫(xiě)的不好,請(qǐng)?jiān)u論,謝謝!

更多學(xué)習(xí)資料請(qǐng)關(guān)注專(zhuān)題《管理系統(tǒng)開(kāi)發(fā)》。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 深入探討Druid動(dòng)態(tài)數(shù)據(jù)源的實(shí)現(xiàn)方式

    深入探討Druid動(dòng)態(tài)數(shù)據(jù)源的實(shí)現(xiàn)方式

    Druid是一個(gè)高性能的實(shí)時(shí)分析數(shù)據(jù)庫(kù),它可以處理大規(guī)模數(shù)據(jù)集的快速查詢(xún)和聚合操作,在Druid中,動(dòng)態(tài)數(shù)據(jù)源是一種可以在運(yùn)行時(shí)動(dòng)態(tài)添加和刪除的數(shù)據(jù)源,使用動(dòng)態(tài)數(shù)據(jù)源,您可以在Druid中輕松地處理不斷變化的數(shù)據(jù)集,本文講給大家介紹一下Druid動(dòng)態(tài)數(shù)據(jù)源該如何實(shí)現(xiàn)
    2023-08-08
  • java正則表達(dá)式用法大全(深度好文)

    java正則表達(dá)式用法大全(深度好文)

    這篇文章主要給大家介紹了關(guān)于java正則表達(dá)式用法大全的相關(guān)資料,正則表達(dá)式在處理字符串時(shí)非常有用,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • Java及數(shù)據(jù)庫(kù)對(duì)日期進(jìn)行格式化方式

    Java及數(shù)據(jù)庫(kù)對(duì)日期進(jìn)行格式化方式

    這篇文章主要介紹了Java及數(shù)據(jù)庫(kù)對(duì)日期進(jìn)行格式化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Spring IOC 注入的3種方式小結(jié)

    Spring IOC 注入的3種方式小結(jié)

    Spring IoC容器支持多種依賴(lài)注入方式,本文主要介紹了Spring IOC 注入的3種方式小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-08-08
  • java使用poi生成excel的步驟

    java使用poi生成excel的步驟

    2010以上格式使用XSSFWorkBook對(duì)象, 2003格式使用HSSFWorkBook對(duì)象, 其他對(duì)象操作基本一樣,本文重點(diǎn)給大家介紹java使用poi生成excel的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2022-04-04
  • Spring @ComponentScan注解掃描組件原理

    Spring @ComponentScan注解掃描組件原理

    這篇文章主要介紹了Spring @ComponentScan自動(dòng)掃描組件使用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-01-01
  • hadoop之MapReduce框架原理

    hadoop之MapReduce框架原理

    這篇文章主要介紹了hadoop的MapReduce框架原理,MapReduce是分為兩個(gè)階段的,MapperTask階段,和ReduceTask階段。如果有感興趣的小伙伴可以借鑒參考
    2023-03-03
  • java中Websocket的使用方法例子

    java中Websocket的使用方法例子

    這篇文章主要給大家介紹了關(guān)于java中Websocket的使用方法,WebSocket是HTML5開(kāi)始提供的一種在瀏覽器和服務(wù)器間進(jìn)行全雙工通信的協(xié)議,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • java并發(fā)編程工具類(lèi)JUC之LinkedBlockingQueue鏈表隊(duì)列

    java并發(fā)編程工具類(lèi)JUC之LinkedBlockingQueue鏈表隊(duì)列

    大家都知道LinkedBlockingQueue 隊(duì)列是BlockingQueue接口的實(shí)現(xiàn)類(lèi),所以它具有BlockingQueue接口的一切功能特點(diǎn),他還提供了兩種構(gòu)造函數(shù),本文中通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2021-06-06
  • Java 兩種延時(shí)thread和timer詳解及實(shí)例代碼

    Java 兩種延時(shí)thread和timer詳解及實(shí)例代碼

    這篇文章主要介紹了Java 兩種延時(shí)thread和timer詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02

最新評(píng)論

巨野县| 迭部县| 德钦县| 德庆县| 鸡泽县| 邓州市| 台南市| 原平市| 大冶市| 龙里县| 邯郸县| 鞍山市| 卢氏县| 库车县| 姚安县| 顺平县| 如东县| 巨野县| 灌云县| 农安县| 彝良县| 读书| 汉中市| 色达县| 龙里县| 冕宁县| 大新县| 汉源县| 阿尔山市| 寻乌县| 康定县| 瑞昌市| 新郑市| 福鼎市| 乳源| 申扎县| 临泉县| 乌恰县| 遂川县| 棋牌| 淅川县|