📄 exampc.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FilenameFilter;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class ExamPC extends Frame implements ItemListener, ActionListener, Runnable {
Choice list;
JTextArea shiTi;// 试题
JTextArea mesg;//答题结果信息区
JTextField showTime;
JButton next,scrose,combit;
JLabel welcomeLabel=null;
ReadTestquestion rt;
CheckboxGroup selectItem;
Checkbox box[];
//
Thread countTime=null;//计时器
long time=0;
boolean closeCountTime=false;//关闭计时器
ExamPC() {
super("exam");
list = new Choice();
// 找出当前目录下的所有.txt文件
String workdir = System.getProperty("user.dir");
File dir = new File(workdir);
FileName fileText = new FileName("txt");
String filename[] = dir.list(fileText);
// 把.txt的文件名加入选题列表框
for (int i = 0; i < filename.length; i++) {
list.add(filename[i]);
}
list.addItemListener(this);
// 试题区
shiTi = new JTextArea(15, 12);
shiTi.setLineWrap(true);
shiTi.setWrapStyleWord(true);
shiTi.setFont(new Font("Times Roman", Font.PLAIN, 14));
shiTi.setForeground(Color.blue);
//消息区
mesg=new JTextArea(10, 10);
mesg.setLineWrap( true);
mesg.setWrapStyleWord( true);
// 答题区
selectItem = new CheckboxGroup();
box = new Checkbox[4];
String s[] = { "A", "B", "C", "D" };
for (int i = 0; i < 4; i++) {
box[i] = new Checkbox(s[i], selectItem, false);
}
combit=new JButton("提交答案");
combit.addActionListener(this);
next = new JButton("读取第一题");
next.addActionListener(this);
scrose=new JButton("查分数");
scrose.addActionListener( this);
rt = new ReadTestquestion();
showTime=new JTextField(20) ;
showTime.setHorizontalAlignment( SwingConstants.RIGHT);
showTime.setEditable( false);
//
countTime=new Thread(this);
// 布局
Box boxH1 = Box.createVerticalBox();
Box boxH2 = Box.createVerticalBox();
Box baseBox = Box.createHorizontalBox();
boxH1.add(new Label("选择试题文件"));
boxH1.add(list);
boxH1.add(new JScrollPane(mesg));
JPanel p4 = new JPanel();
boxH1.add(scrose);
p4.add(new Label("剩余时间"));
p4.add(showTime);
boxH1.add(p4);
boxH2.add(new Label("试题内容:"));
boxH2.add(new JScrollPane(shiTi));
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
for (int i = 0; i < 4; i++) {
p3.add(box[i]);
}
p2.add(p3);
p2.add(combit);
p2.add(next);
boxH2.add(p2);
baseBox.add(boxH1);
baseBox.add(boxH2);
setLayout(new BorderLayout());
add(baseBox, BorderLayout.CENTER);
welcomeLabel=new JLabel("欢迎使用本考试系统!");
welcomeLabel.setFont( new Font("隶书",Font.PLAIN ,30));
welcomeLabel.setHorizontalAlignment( SwingConstants.CENTER );
welcomeLabel.setForeground (Color.blue );
add(welcomeLabel,BorderLayout.NORTH );
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent w) {
System.exit(0);
}
});
setVisible(true);
setBounds(60, 50, 660, 460);
setResizable(false);
validate();
}
public static void main(String[] args) {
ExamPC ep = new ExamPC();
}
public void itemStateChanged(ItemEvent e) {
showTime.setText( null);
closeCountTime=false;
String name = (String) list.getSelectedItem();
String str="";
rt.setFileName(name);
//
rt.setcomplete( false);
time=rt.getTime() ;
if(countTime.isAlive())
{
closeCountTime=true;
countTime.interrupt() ;
}
countTime=new Thread(this);
mesg.setText( null);
shiTi.setText(null);
next.setText ("读取第一题" );
combit.setEnabled( false);
next.setEnabled( true);
str=rt.getFilename();
welcomeLabel.setText( "欢迎考试,你选择的是:试题"+str.substring( 0,str.length() -4));
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == next) {
next.setText( "下一题");
combit.setEnabled( true);
shiTi.setText(rt.getTestContent());
mesg.setText( null);
next.setEnabled( false);
//
try {
countTime.start() ;
} catch (Exception e1) {
}
}
if(e.getSource() ==combit)
{
next.setEnabled( true);
combit.setEnabled( false);
String answer="?";
for(int i=0;i<4;i++)
{
if(box[i].getState() ==true)
{answer=box[i].getLabel() ;
box[i].setState(false);
break;
}
}
rt.setSelection( answer);
}
if (e.getSource() ==scrose)
{
int score=rt.getScore() ;
String message=rt.getMessage() ;
mesg.setText( message+"\n"+"你答对了:"+score+"个题");
}
}
public void run()
{
while(true)
{
if(time<=0)
{
closeCountTime=true;
countTime.interrupt() ;
combit.setEnabled( false);
next.setEnabled( false);
showTime.setText( "考试时间用尽,考试结束!");
}
else
if(rt.getcomplete() )
{
closeCountTime=true;
countTime.interrupt() ;
combit.setEnabled( false);
next.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;
showTime.setText( ""+leftHour+" 小时"+leftMinute+"分"+leftSecond+"秒");
}
try {
Thread.sleep( 1000);
} catch (InterruptedException e)
{
if(closeCountTime==true)
return;
}
}
}
}
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 + -