电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:3日期:2022-08-31 11:04:37

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

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

源碼Testquestion 類

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; //答對和答錯 ClockDispaly mt; //倒計時模塊 public Test2(){ this.setTitle('學(xué)生在線考試系統(tǒng)v1'); //設(shè)置標(biāo)題 this.setSize(440,320); //設(shè)置窗口大小 this.setLocationRelativeTo(null); //設(shè)置顯示位置居中 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設(shè)置關(guān)閉時關(guān)閉 panel = new JPanel(); //初始化面板 panel2 = new JPanel(); panel3 = new JPanel(); label = new JLabel('總考試時間: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(); //初始化選項組 start = new JButton('開始考試'); //初始化按鍵 back = new JButton('上一題'); next = new JButton('下一題'); commit = new JButton('提交考試'); aButton.addActionListener(this); //單選按鈕添加監(jiān)聽事件 bButton.addActionListener(this); cButton.addActionListener(this); dButton.addActionListener(this); start.addActionListener(this); //按鈕添加監(jiān)聽事件 back.addActionListener(this); next.addActionListener(this); commit.addActionListener(this); buttonGroup.add(aButton); //把單選按鈕放到選項組 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è)置窗口可見 mt = new ClockDispaly(clock, 30); //調(diào)用并設(shè)置倒計時的時間 } public void createExam() {//創(chuàng)建考試模塊 Vector<Testquestion> qList=null;//創(chuà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)備接收一個題目的內(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)計試題數(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è)置成績模塊 right=0;error=0; for (int i = 0; i < topicnum; i++) { if (questions[i].check()) {//判斷答案的正確與錯誤 right++; }else { error++; } } int score = (int)(right*100/topicnum); //設(shè)置分?jǐn)?shù) JOptionPane.showMessageDialog(null, '答對'+right+'題,答錯'+error+'題,分?jǐn)?shù)為'+score); } @Override public void actionPerformed(ActionEvent e) {//動作監(jiān)聽事件 if (e.getSource()==start) {//開始開始按鍵實(shí)現(xiàn) createExam(); //調(diào)用createExam模塊 p=0; //題目序號 showQuestion(); //調(diào)用showQuestion模塊 start.setEnabled(false);//設(shè)置按鈕不可點(diǎn)擊 mt.start(); //考試時間倒計時啟動 } 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考試倒計時模塊 private JLabel lefttimer; private int testtime; public ClockDispaly(JLabel lt,int time) { lefttimer = lt; testtime = time * 60; } public void run(){ NumberFormat numberFormat = NumberFormat.getInstance();//控制時間的顯示格式 numberFormat.setMinimumIntegerDigits(2);//設(shè)置數(shù)值的整數(shù)部分允許的最小位數(shù) int h,m,s;//定義時分秒 while (testtime >= 0) { h = testtime / 3600; m = testtime % 3600 / 60; s = testtime % 60; StringBuffer stringBuffer = new StringBuffer(''); //增加到lefttimer標(biāo)簽 stringBuffer.append('考試剩余時間為:'+numberFormat.format(h)+':'+numberFormat.format(m)+':'+numberFormat.format(s)); lefttimer.setText(stringBuffer.toString()); try { Thread.sleep(1000);//延時一秒 } catch (Exception e) { //ignore error } testtime = testtime - 1; } if (testtime <= 0) { JOptionPane.showMessageDialog(null, '考試結(jié)束'); System.exit(0); } }}

txt文件

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

效果圖

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

正在嘗試寫博客,如寫的不好,請評論,謝謝!

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 标策网-专注公司商业知识服务、助力企业发展 | 广东恩亿梯电源有限公司【官网】_UPS不间断电源|EPS应急电源|模块化机房|电动汽车充电桩_UPS电源厂家(恩亿梯UPS电源,UPS不间断电源,不间断电源UPS) | 阳光1号桔柚_无核沃柑_柑橘新品种枝条苗木批发 - 苧金网 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 对辊式破碎机-对辊制砂机-双辊-双齿辊破碎机-巩义市裕顺机械制造有限公司 | 深圳网站建设-高端企业网站开发-定制网页设计制作公司 | 置顶式搅拌器-优莱博化学防爆冰箱-磁驱搅拌器-天津市布鲁克科技有限公司 | 小型UV打印机-UV平板打印机-大型uv打印机-UV打印机源头厂家 |松普集团 | 实验室pH计|电导率仪|溶解氧测定仪|离子浓度计|多参数水质分析仪|pH电极-上海般特仪器有限公司 | 发电机组|柴油发电机组-批发,上柴,玉柴,潍柴,康明斯柴油发电机厂家直销 | 天津力值检测-天津管道检测-天津天诚工程检测技术有限公司 | 万博士范文网-您身边的范文参考网站Vanbs.com | 武汉天安盾电子设备有限公司 - 安盾安检,武汉安检门,武汉安检机,武汉金属探测器,武汉测温安检门,武汉X光行李安检机,武汉防爆罐,武汉车底安全检查,武汉液体探测仪,武汉安检防爆设备 | 幂简集成 - 品种超全的API接口平台, 一站搜索、试用、集成国内外API接口 | 镀锌角钢_槽钢_扁钢_圆钢_方矩管厂家_镀锌花纹板-海邦钢铁(天津)有限公司 | 煤棒机_增碳剂颗粒机_活性炭颗粒机_木炭粉成型机-巩义市老城振华机械厂 | 注塑模具_塑料模具_塑胶模具_范仕达【官网】_东莞模具设计与制造加工厂家 | 山楂片_雪花_迷你山楂片_山楂条饼厂家-青州市丰源食品厂 | 高考志愿规划师_高考规划师_高考培训师_高报师_升学规划师_高考志愿规划师培训认证机构「向阳生涯」 | 高效复合碳源-多核碳源生产厂家-污水处理反硝化菌种一长隆科技库巴鲁 | 包装机_厂家_价格-山东包装机有限公司 | 钢格板_钢格栅_格栅板_钢格栅板 - 安平县鑫拓钢格栅板厂家 | 自动气象站_气象站监测设备_全自动气象站设备_雨量监测站-山东风途物联网 | 成都亚克力制品,PVC板,双色板雕刻加工,亚克力门牌,亚克力标牌,水晶字雕刻制作-零贰捌广告 | 万博士范文网-您身边的范文参考网站Vanbs.com | 贵州自考_贵州自学考试网| 楼梯定制_楼梯设计施工厂家_楼梯扶手安装制作-北京凌步楼梯 | 冷水机,风冷冷水机,水冷冷水机,螺杆冷水机专业制造商-上海祝松机械有限公司 | 滚筒烘干机_转筒烘干机_滚筒干燥机_转筒干燥机_回转烘干机_回转干燥机-设备生产厂家 | 工业铝型材生产厂家_铝合金型材配件批发精加工定制厂商 - 上海岐易铝业 | 定量包装机,颗粒定量包装机,粉剂定量包装机,背封颗粒包装机,定量灌装机-上海铸衡电子科技有限公司 | 中央空调维修、中央空调保养、螺杆压缩机维修-苏州东菱空调 | 色油机-色母机-失重|称重式混料机-称重机-米重机-拌料机-[东莞同锐机械]精密计量科技制造商 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 微水泥_硅藻泥_艺术涂料_艺术漆_艺术漆加盟-青岛泥之韵环保壁材 武汉EPS线条_EPS装饰线条_EPS构件_湖北博欧EPS线条厂家 | 水热合成反应釜-防爆高压消解罐-西安常仪仪器设备有限公司 | 上海办公室装修,写字楼装修—启鸣装饰设计工程有限公司 | 净化板-洁净板-净化板价格-净化板生产厂家-山东鸿星新材料科技股份有限公司 | 青岛侦探调查_青岛侦探事务所_青岛调查事务所_青岛婚外情取证-青岛狄仁杰国际侦探公司 | 淘气堡_室内儿童乐园_户外无动力儿童游乐设备-高乐迪(北京) | 东莞市踏板石餐饮管理有限公司_正宗桂林米粉_正宗桂林米粉加盟_桂林米粉加盟费-东莞市棒子桂林米粉 |