本文实例为大家分享了Java swing读取txt文件实现学生考试系统的具体代码,供大家参考,具体内容如下
主要实现了一个简单的倒计时答题系统
源码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;//设置题目数指针
int topicnum=0;
int right,error;//答对和答错
ClockDispaly mt;//倒计时模块
public Test2(){
this.setTitle("学生在线考试系统v1"); //设置标题
this.setSize(440,320); //设置窗口大小
this.setLocationRelativeTo(null); //设置显示位置居中
this.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); //设置关闭时关闭
panel = new JPanel(); //初始化面板
panel2 = new JPanel();
panel3 = new JPanel();
label = new JLabel("总考试时间:100分钟 "); //初始化并命名标签
clock = new JLabel();
jtextarea = new Jtextarea(10,35); //初始化文本区域
jtextarea.setEditable(false); //设置文本不可修改
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); //单选按钮添加监听事件
bButton.addActionListener(this);
cButton.addActionListener(this);
dButton.addActionListener(this);
start.addActionListener(this); //按钮添加监听事件
back.addActionListener(this);
next.addActionListener(this);
commit.addActionListener(this);
buttonGroup.add(aButton); //把单选按钮放到选项组
buttonGroup.add(bButton);
buttonGroup.add(cButton);
buttonGroup.add(dButton);
panel.add(label); //把标签放入面板panel
panel.add(clock);
panel.add(start); //把按键放入面板panel
panel2.add(jtextarea); //把文本区域放入面板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); //设置面板panel放在上面
this.add(panel2,BorderLayout.CENTER); //设置面板panel2放在中间
this.add(panel3, BorderLayout.SOUTH); //设置面板panel放在下面
this.setVisible(true); //设置窗口可见
mt = new ClockDispaly(clock, 30); //调用并设置倒计时的时间
}
public void createExam() {//创建考试模块
Vector qList=null;//创建一个向量列表,用于动态增加试题
Testquestion t;
String questionText="";
String standardKey;
String s;
try {
FileReader fr=new FileReader("D:\test.txt");
BufferedReader br = new BufferedReader(fr); //可以每次读一行
qList=new Vector();
while((s=br.readLine())!=null){//读取试题
if (s.equals("*****")){
questionText="";//准备接收一个题目的内容
s = br.readLine();//获取试题内容的首行
}
if (s.equals("$$$$$")){//准备读取试题的答案
s = br.readLine(); //获取试题的答案
standardKey = s; //把试题答案赋值给正确答案
t = new Testquestion(questionText,standardKey); //把试题和答案赋值给t
qList.add(t); //把试题和答案赋值给列表
}
questionText=questionText+s+'n';
}
br.close();//关闭缓冲流
fr.close();//关闭文件流
}
catch (IOException e) {
e.printStackTrace(); //打印异常信息
}
topicnum=qList.size(); //统计试题数量
questions=new Testquestion[topicnum];
for (int i=0;i= 0) {
h = testtime / 3600;
m = testtime % 3600 / 60;
s = testtime % 60;
StringBuffer stringBuffer = new StringBuffer("");
//增加到lefttimer标签
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, "考试结束");
System.exit(0);
}
}
}
txt文件
效果图
正在尝试写博客,如写的不好,请评论,谢谢!
更多学习资料请关注专题《管理系统开发》。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



