1、主类Test
package cn.kong;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class Test extends JFrame {
TestArea testPanel = null;
Container con = null;
public Test(){
super("标准化模拟考试");
testPanel = new TestArea();
con = getContentPane();
con.add(testPanel,BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowListener() {
public void windowOpened(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
JDialog 弹出窗口 = new JDialog(Test.this,true);
弹出窗口.setTitle("离开考试");
JLabel 标签 = new JLabel("同学,你确定退出考试吗?",JLabel.CENTER);
标签.setFont(new Font("宋体",Font.BOLD,14));
标签.setForeground(Color.red);
弹出窗口.add(标签,BorderLayout.CENTER);
JPanel j1 = new JPanel();
final JButton 退出 = new JButton("退出");
final JButton 取消 = new JButton("取消");
退出.setForeground(Color.blue);
取消.setForeground(Color.blue);
j1.add(退出);
j1.add(取消);
弹出窗口.add(j1,BorderLayout.SOUTH);
退出.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
取消.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
弹出窗口.dispose();
}
});
弹出窗口.setSize(300,200);
弹出窗口.setLocation(800,400);
弹出窗口.setVisible(true);
弹出窗口.setForeground(Color.BLUE);
}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) { }
});
setVisible(true);
setBounds(250,150,1200,800);
con.validate();
validate();
}
public static void main(String[] args) {
new Test();
}
}
2、ReadTestQuestion类
package cn.kong;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.StringTokenizer;
public class ReadTestQuestion {
String filename="";
String correctAnswer = "";
String testContent = "";
String selection = "";
int score = 0;
long time = 0;
boolean 完成考试 = false;
File f = null;
FileReader in = null;
BufferedReader 读取 = null;
public void setFilename(String name){
filename = name;
score = 0;
selection="";
try{
if(in != null && 读取 != null ){
in.close();
读取.close();
}
f = new File(filename);
in = new FileReader(f);
读取 = new BufferedReader(in);
correctAnswer = (读取.readLine()).trim();
String temp = (读取.readLine()).trim();
StringTokenizer token = new StringTokenizer(temp,":");
int hour = Integer.parseInt(token.nextToken());
int minute = Integer.parseInt(token.nextToken());
int second = Integer.parseInt(token.nextToken());
time = 1000*(second+minute*60+hour*60*60);
}catch(Exception e){
testContent="没有选则试题";
}
}
public String getFilename(){
return filename;
}
public long getTime(){ return time; }
public void set完成考试(boolean b){ 完成考试 = b; }
public boolean get完成考试(){ return 完成考试; }
public String getTestContent(){
try{
String s = null;
StringBuffer temp = new StringBuffer();
if(读取!= null){
while((s = 读取.readLine()) != null){
if(s.startsWith("**"))
break;
temp.append("\n"+s);
if(s.startsWith("endend")){
in.close();
读取.close();
完成考试=true;
}
}
testContent = new String(temp);
}else{
testContent = new String("没有选则试题");
}
}catch(Exception e){
testContent = "试题内容为空,考试结束!!";
}
return testContent;
}
public void setSelection(String s){
selection = selection + s;
}
public int getScore(){
score = 0;
int length1 = selection.length();
int length2 = correctAnswer.length();
int min = Math.min(length1,length2);
for(int i = 0 ; i < min; i++){
try{
if(selection.charAt(i) == correctAnswer.charAt(i)){
score++;
}
}catch(StringIndexOutOfBoundsException e){
i = 0;
}
}
return score;
}
public String getMessages(){
int length1 = selection.length();
int length2 = correctAnswer.length();
int length = Math.min(length1,length2);
String message = "正确答案:"+correctAnswer.substring(0,length)+"\n"+"你的回答:"+selection+"\n";
return message;
}
}
3、TestArea类
package cn.kong;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.*;
class FileName implements FilenameFilter{
String str = null;
public FileName(String s){
str = "."+s;
}
public boolean accept(File dir, String name){
return name.endsWith(str);
}
}
public class TestArea extends JPanel implements ActionListener, ItemListener,Runnable {
Choice list = null;
JTextArea 试题显示区 = null,消息区 = null;
JRadioButton box[];
JButton 提交该题答案,读取下一题,查看分数;
ReadTestQuestion 读取试题 = null;
JLabel welcomeLabel = null;
Thread countTime = null;
long time = 0;
boolean 是否关闭计时器 = false;
boolean 是否暂停计时 = false;
JTextField timeShow = null;
JButton 暂停或继续计时 = null;
int flag = 0, flag1 = 0;
public TestArea(){
list = new Choice();
list.add("Please choice examination");
String 当前目录 = System.getProperty("user.dir");
File dir = new File(当前目录);
FileName fileText = new FileName("txt");
String fileName[] = dir.list(fileText);
for(int i = 0; i < fileName.length;i++){
list.add(fileName[i]);
}
试题显示区 = new JTextArea(15,12);
试题显示区.setLineWrap(true);
试题显示区.setWrapStyleWord(true);
试题显示区.setFont(new Font("TimesRoman",Font.PLAIN,20));
试题显示区.setForeground(Color.BLUE);
试题显示区.setEditable(false);
消息区 = new JTextArea(8,8);
消息区.setForeground(Color.blue);
消息区.setLineWrap(true);
消息区.setWrapStyleWord(true);
消息区.setEditable(false);
消息区.setFont(new Font("TimesRoman",Font.PLAIN,20));
ButtonGroup group = new ButtonGroup();
countTime = new Thread(this);
String s[] = {"A","B","C","D"};
box = new JRadioButton[4];
for(int i = 0; i < 4; i++){
box[i] = new JRadioButton(s[i]);
group.add(box[i]);
}
暂停或继续计时 = new JButton("暂停计时");
暂停或继续计时.addActionListener(this);
提交该题答案 = new JButton("提交该题答案");
读取下一题 = new JButton("读取第一题");
读取下一题.setForeground(Color.blue);
提交该题答案.setForeground(Color.blue);
查看分数 = new JButton("查看分数");
查看分数.setForeground(Color.blue);
提交该题答案.setEnabled(false);
提交该题答案.addActionListener(this);
读取下一题.addActionListener(this);
查看分数.addActionListener(this);
list.addItemListener(this);
读取试题 = new ReadTestQuestion();
JPanel pAddbox = new JPanel() ;
for(int i = 0;i < 4; i++){
pAddbox.add(box[i]);
}
Box boxH1 = Box.createVerticalBox();
Box boxH2 = Box.createVerticalBox();
Box baseBox = Box.createHorizontalBox();
boxH1.add(new JLabel("选择试题文件"));
boxH1.add(list);
boxH1.add(new JScrollPane(消息区));
boxH1.add(查看分数);
timeShow = new JTextField(20);
timeShow.setHorizontalAlignment(SwingConstants.RIGHT);
timeShow.setEditable(false);
JPanel p1 = new JPanel();
p1.add(new JLabel("剩余时间"));
p1.add(timeShow);
p1.add(暂停或继续计时);
boxH1.add(p1);
boxH2.add(new JLabel("试题内容:"));
boxH2.add(new JScrollPane(试题显示区));
JPanel p2 = new JPanel();
p2.add(pAddbox);
p2.add(提交该题答案);
p2.add(读取下一题);
boxH2.add(p2);
baseBox.add(boxH1);
baseBox.add(boxH2);
setLayout(new BorderLayout());
add(baseBox,BorderLayout.CENTER);
welcomeLabel = new JLabel("欢迎来到考试系统,祝大家考试顺利",JLabel.CENTER);
welcomeLabel.setFont((new Font("隶书",Font.PLAIN,24)));
welcomeLabel.setForeground(Color.blue);
add(welcomeLabel,BorderLayout.NORTH);
}
public void itemStateChanged(ItemEvent e) {
timeShow.setText(null);
是否关闭计时器 = false;
是否暂停计时 = false;
暂停或继续计时.setText("暂停计时");
String name = (String)list.getSelectedItem();
读取试题.setFilename(name);
读取试题.set完成考试(false);
time = 读取试题.getTime();
if(countTime.isAlive()){
是否关闭计时器 = true;
countTime.interrupt();
}
countTime = new Thread(this);
消息区.setText(null);
试题显示区.setText(null);
读取下一题.setText("读取第一题");
提交该题答案.setEnabled(false);
读取下一题.setEnabled(true);
welcomeLabel.setText("欢迎考试,你选择的试题是:"+读取试题.getFilename());
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == 读取下一题){
读取下一题.setText("读取下一题");
提交该题答案.setEnabled(true);
String contentTest = 读取试题.getTestContent();
试题显示区.setText(contentTest);
消息区.setText(null);
读取下一题.setEnabled(false);
try{
countTime.start();
}catch(Exception event){
event.printStackTrace();
}
}
if(e.getSource() == 提交该题答案){
读取下一题.setEnabled(true);
提交该题答案.setEnabled(false);
String answer = "?";
for(int i = 0; i < 4; i++){
if(box[i].isSelected()){
answer = box[i].getText();
box[i].setSelected(false);
break;
}
}
读取试题.setSelection(answer);
}
if(e.getSource() == 查看分数){
if(flag == 1|| flag1 == 1){
int score = 读取试题.getScore();
String messages = 读取试题.getMessages();
消息区.setText("分数:"+score+"\n"+messages);
}else{
消息区.setText("对不起,你还没有答完题,不能查看分数");
}
}
if(e.getSource()==暂停或继续计时){
if(是否暂停计时 == false){
暂停或继续计时.setText("继续计时");
是否暂停计时=true;
}else if(是否暂停计时 == true){
暂停或继续计时.setText("暂停计时");
是否暂停计时= false;
countTime.interrupt();
}
}
}
public synchronized void run() {
while(true){
if(time <= 0){
是否关闭计时器 = true;
countTime.interrupt();
提交该题答案.setEnabled(false);
读取下一题.setEnabled(false);
timeShow.setText("用时尽,考试结束");
flag = 1;
}else if(读取试题.get完成考试()){
是否关闭计时器=true;
timeShow.setText("考试效果:分数*剩余时间(秒)="+1.0*读取试题.getScore()*(time/1000));
countTime.interrupt();
提交该题答案.setEnabled(false);
读取下一题.setEnabled(false);
flag1 = 1;
}else if(time >= 1){
time = time - 1000;
long leftTime = time/1000;
long leftHour = leftTime/3600;
long leftMinute = (leftTime - leftHour*3600)/60;
long leftSecond = leftTime % 60;
timeShow.setText(""+leftHour+"小时"+leftMinute+"分钟"+leftSecond+"秒");
}
try{
Thread.sleep(1000);
} catch (InterruptedException e) {
if(是否关闭计时器 == true){
return;
}
}
while(是否暂停计时 == true){
try{
提交该题答案.setEnabled(false);
读取下一题.setEnabled(false);
wait();
}catch(InterruptedException e){
if(是否暂停计时 == false){
提交该题答案.setEnabled(true);
读取下一题.setEnabled(true);
notify();
}
}
}
}
}
}