📄 testpanel.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/**定义一个主面板类
*
*/
class TestPanel extends JPanel implements ActionListener,ItemListener,Runnable
{
//boolean boo=true;
boolean isCloseTime,isPauseTime;//定义两个布尔型变量,控制线程的执行
ReadTestquestion readTest=null;//负责读取试题和判断分数的对象
Thread countTime=null;//计时器对象
long time=0;//创建时间对象
String contentTest=new String();
JLabel welLabel,
choiceLabel,
contentLabel,
leftLabel;//定义四个标签对象
JComboBox testChoice;//定义下拉列表对象
JTextArea answerArea,contentArea;//定义答案文本区和试题文本区
JButton showButton,
timeButton,
submitButton,
readButton;//定义四个按钮
JTextField timeText,testText;//定义时间文本条和试题名称文本条
JRadioButton button[];//定义单选按钮数组
ButtonGroup answer;//定义按钮组
JPanel panel1,panel2,panel3,panel4;//定义四个面板
Box boxH1,boxH2,boxH3,boxV2,boxV3,boxH4;//定义4个行型盒式容器和2个列型盒式容器
TestPanel()//面板构造方法
{
setLayout(new BorderLayout());
readTest=new ReadTestquestion();
testChoice=new JComboBox();//创建下拉列表对象
String Path=System.getProperty("user.dir");//获取路径
File dir=new File(Path);
FileName fileTxt=new FileName("txt");
String fileName[]=dir.list(fileTxt);//将下拉列表的文件名赋给数组
for(int i=0;i<fileName.length;i++)
{
testChoice.addItem(fileName[i]);//添加符合条件的文件名
}
testChoice.setSelectedItem("");
testChoice.setBackground(Color.RED);
welLabel=new JLabel("欢迎考试,请选择试题:",new ImageIcon("huanying.gif"),JLabel.CENTER);
choiceLabel=new JLabel("请从下表中选择试题文件");
contentLabel=new JLabel("本试题内容如下");
leftLabel=new JLabel("剩余时间为:");//创建四个标签对象
answerArea=new JTextArea(20,30);
answerArea.setEditable(false);
contentArea=new JTextArea(23,40);
contentArea.setEditable(false);//创建两个文本区
showButton=new JButton("查看分数");
timeButton=new JButton(" 暂停计时");
submitButton=new JButton("提交答案");
readButton=new JButton("开始答题");//创建四个按钮对象
showButton.setEnabled(false);
timeButton.setEnabled(false);
submitButton.setEnabled(false);
readButton.setEnabled(false);
testText=new JTextField("请选题",5);testText.setEditable(false);
timeText=new JTextField();timeText.setEditable(false);
answer=new ButtonGroup();
button=new JRadioButton[4];//创建单选按钮数组
String s[]={"A","B","C","D"};
for(int i=0;i<4;i++)
{
button[i]=new JRadioButton(s[i]);
answer.add(button[i]);
button[i].setEnabled(false);
}
testChoice.addItemListener(this);//下拉列表添加监听
showButton.addActionListener(this);
timeButton.addActionListener(this);//显示时间和得分按钮添加监听
submitButton.addActionListener(this);
readButton.addActionListener(this);//提交按钮和读取下一题按钮添加监听
panel1=new JPanel();
panel2=new JPanel();
panel3=new JPanel();
panel4=new JPanel();//创建四块面板
boxH1=Box.createHorizontalBox();//创建行型布局
boxH1.add(welLabel);
boxH1.add(testText);
panel1.add(boxH1);
boxH2=Box.createHorizontalBox();//创建行型布局
boxH2.add(leftLabel);
boxH2.add(timeText);
boxV2=Box.createVerticalBox();//创建列型布局
boxV2.add(choiceLabel);
boxV2.add(testChoice);
boxV2.add(Box.createVerticalStrut(8));//垂直支撑
boxV2.add(answerArea);
boxV2.add(Box.createVerticalStrut(8));//垂直支撑
boxV2.add(showButton);
boxV2.add(Box.createVerticalStrut(8));//垂直支撑
boxV2.add(boxH2);
panel2.add(boxV2);
boxH3=Box.createHorizontalBox();//创建行型布局
boxH3.add(button[0]);
boxH3.add(Box.createHorizontalStrut(10));//水平支撑
boxH3.add(button[1]);
boxH3.add(Box.createHorizontalStrut(10));//水平支撑
boxH3.add(button[2]);
boxH3.add(Box.createHorizontalStrut(10));//水平支撑
boxH3.add(button[3]);
boxV3=Box.createVerticalBox();//创建列型布局
boxV3.add(contentLabel);
boxV3.add(Box.createVerticalStrut(8));//垂直支撑
boxV3.add(contentArea);
boxV3.add(Box.createVerticalStrut(8));//垂直支撑
boxV3.add(boxH3);
panel3.add(boxV3);
boxH4=Box.createHorizontalBox();//创建行型布局
boxH4.add(Box.createHorizontalStrut(20));//水平支撑
boxH4.add(timeButton);
boxH4.add(Box.createHorizontalStrut(300));//水平支撑
boxH4.add(submitButton);
boxH4.add(Box.createHorizontalStrut(60));//水平支撑
boxH4.add(readButton);
boxH4.add(Box.createHorizontalStrut(20));//水平支撑
panel4.add(boxH4);
add(panel1,BorderLayout.NORTH);//把面板1添加到上部
add(panel2,BorderLayout.WEST);//把面板2添加到左部
add(panel3,BorderLayout.CENTER);//把面板3添加到中部
add(panel4,BorderLayout.SOUTH);//把面板4添加到下部
}
public void itemStateChanged(ItemEvent e)//对下拉列表进行监听
{
timeText.setText(null);
isCloseTime=false;
isPauseTime=false;
readButton.setText("开始答题");
timeButton.setText("暂停计时");
String name=(String)testChoice.getSelectedItem();
readTest.setFileName(name);//readTest负责读取试题和判断分数的对象
readTest.setTestOver(false);//设置考试时间为false
time=readTest.getTime();//获得时间
countTime=new Thread(this);//创建线程
answerArea.setText(null);
contentArea.setText(null);
submitButton.setEnabled(false);
readButton.setEnabled(true);
for(int i=0;i<4;i++)
{
button[i].setEnabled(false);//设置答案选项为不可选
}
testText.setText(readTest.getFileName());//设置选取试题文件的名字
}
public void actionPerformed(ActionEvent e)//对所有按钮添加监听
{
if(e.getSource()==readButton)//如果读取下一题促发监听事件
{
readButton.setText("读下一题");
submitButton.setEnabled(true);
showButton.setEnabled(true);
timeButton.setEnabled(true);
for(int i=0;i<4;i++)
{
button[i].setEnabled(true);//设置答案选项为可选
}
contentTest=readTest.getTestContent();//用TestArea的方法读取试题内容
contentArea.setText(contentTest);//显示试题内容
answerArea.setText(null);
if(readTest.TestOver)
{
showButton.setEnabled(true);
}
try
{
countTime.start();//启动线程
}
catch(Exception ex)
{
}
}
if(e.getSource()==submitButton)//提交按钮促发监听事件
{
readButton.setEnabled(true);
submitButton.setEnabled(false);
String answer="";
for(int i=0;i<4;i++)
{
if(button[i].isSelected())//判断那个答案被选中
{
answer=button[i].getText();
button[i].setSelected(false);
break;
}
}
readTest.setSelection(answer);//存入答案选项
}
if(e.getSource()==showButton)//如果显示分数按钮促发事件
{
int score=readTest.getScore();//得到分数
String messages=readTest.getMessage();//获取考生答案信息
answerArea.setText("分数:"+score+"\n"+messages);//设置答案文本区显示内容
showButton.setEnabled(false);//设置为不可用
}
if(e.getSource()==timeButton)//时间按钮促发事件
{
if(isPauseTime==false)
{
timeButton.setText("继续计时");//把时间按钮文本变为"继续计时"
isPauseTime=true;
contentArea.setText("请按继续计时按钮开始继续做题!");
}
else if(isPauseTime==true)
{
timeButton.setText("暂停计时");//把时间按钮文本变为"暂停计时"
isPauseTime=false;
countTime.interrupt();
contentArea.setText(contentTest);
}
}
}
public synchronized void run()//线程启动时调用run方法
{
while(true)
{
if(time<=0)
{
isCloseTime=true;
countTime.interrupt();//使线程消亡
readButton.setEnabled(false);
submitButton.setEnabled(false);
timeText.setText("考试时间已用尽,考试结束!");
}
else if(readTest.getTestOver())//判断考试是否结束
{
isCloseTime=true;
countTime.interrupt();//使线程消亡
submitButton.setEnabled(false);
readButton.setEnabled(false);
}
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;
timeText.setText(""+leftHour+"小时"+leftMinute+"分钟"+leftSecond+"秒");//得到时间的显示
}
try
{
Thread.sleep(1000);//使线程睡1秒
}
catch(InterruptedException ex1)
{
if(isCloseTime==true)
{
return;//使线程消亡
}
}
while(isPauseTime==true)
{
try
{
wait();//使线程等待
}
catch(InterruptedException ex2)
{
if(isCloseTime==false)
{
notifyAll();//唤醒线程
}
}
}
}
}
/**创建一个实现过滤流接口的类
*
*/
class FileName implements FilenameFilter
{
String str=null;
FileName(String s)
{
str="."+s;//设置试题文件的后缀名
}
public boolean accept(File dir,String name)//检查文件中符合已指定后缀结尾的文件名方法
{
return name.endsWith(str);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -