📄 testarea.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class TestArea extends JPanel implements ActionListener,ItemListener,Runnable
{ Choice llist=null; //下拉列表 选择习题
JTextArea testArea,massegeArea;
JCheckBox box[]; //复选框 用来选择答案
JButton upAnswer,readNext,lookScore;
JLabel welcomeLabel;
//ReadTestquestion readTest=null;
Thread countTime=null;
String strNumber,nameTest,answer="?",answers="",selection="",name="TEST1";
Connection conn;
Statement stt;
ResultSet res;
int num=0,i=1,score=0;
boolean 完成考试=false;
long time=1000*(1*60*60+30*60);;
JTextField timeShow=null;
//TestSystem student=null;
boolean 是否关闭计时器=false,
是否暂停计时=false;
JButton 暂停或继续计时=null;
public TestArea()
{ llist= new Choice();
llist.add("TEST1");
llist.add("TEST2");
llist.addItemListener(this);
testArea=new JTextArea(15,12);//试题显示区
testArea.setEditable(false);
testArea.setLineWrap(true);
testArea.setWrapStyleWord(true);
testArea.setFont(new Font("TimesRoman",Font.PLAIN,14));
testArea.setForeground(Color.blue);
massegeArea=new JTextArea(8,8);//消息区
massegeArea.setEditable(false);
massegeArea.setForeground(Color.blue);
massegeArea.setLineWrap(true);
massegeArea.setWrapStyleWord(true);
countTime=new Thread(this); //
String s[]={"A","B","C","D"};
box=new JCheckBox[4];
for(int i=0;i<4;i++)
{
box[i]=new JCheckBox(s[i]);
}
/*////////数据库的连接///////////////
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}//try
catch(ClassNotFoundException e){}
try
{ conn=DriverManager.getConnection("jdbc:odbc:SUNJava","txl2008","2008");
stt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
}//try
catch(SQLException ex){}
catch(Exception ex){}
//////////
//////////数据库///////////
//////////
try
{ for(int j=2;j<=num;j++)
{ res.absolute(j);
selection=selection+(res.getString(3)).trim();
System.out.println("selection="+selection);
}
}//try
catch(Exception ex){}
//////////////////////////////////*/
暂停或继续计时=new JButton("暂停计时");
暂停或继续计时.addActionListener(this);
upAnswer=new JButton("提交该题答案");//提交该题答案
upAnswer.setForeground(Color.blue);
upAnswer.setEnabled(false);
upAnswer.addActionListener(this);
readNext=new JButton("读取第一题");//读取下一题
readNext.setForeground(Color.blue);
readNext.addActionListener(this);
lookScore=new JButton("查看分数");//查看分数
lookScore.addActionListener(this);
lookScore.setForeground(Color.blue);
llist.addItemListener(this);
//readTest=new ReadTestquestion();
JPanel pAddbox=new JPanel();
for(int i=0;i<4;i++)
{
pAddbox.add(box[i]);
}
Box boxH1=Box.createVerticalBox(),
boxH2=Box.createVerticalBox(),
baseBox=Box.createHorizontalBox();
boxH1.add(new JLabel("选择试题文件"));
boxH1.add(llist);
boxH1.add(new JScrollPane(massegeArea));
boxH1.add(lookScore);
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(testArea));
JPanel p2=new JPanel();
p2.add(pAddbox);
p2.add(upAnswer);
p2.add(readNext);
boxH2.add(p2);
baseBox.add(boxH1);
baseBox.add(boxH2);
setLayout(new BorderLayout());
add(baseBox,BorderLayout.CENTER);
//student=new TestSystem();
welcomeLabel=new JLabel("欢迎考试,提高英语水平",JLabel.CENTER);
welcomeLabel.setFont(new Font("隶书",Font.PLAIN,24));
welcomeLabel.setForeground(Color.blue);
add(welcomeLabel,BorderLayout.NORTH);
}//public TestArea()
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==llist)
{ name=llist.getSelectedItem();
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}//try
catch(ClassNotFoundException ex){}
try
{ conn=DriverManager.getConnection("jdbc:odbc:Java","","");
stt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
res=stt.executeQuery("SELECT * FROM "+name);
res.last();
num=res.getRow();
for(int j=2;j<=num;j++)
{ res.absolute(j);
selection=selection+(res.getString(3)).trim();
System.out.println("selection="+selection);
}
res.absolute(1);
String temp=(res.getString(2)).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);
System.out.println("hour="+hour);
}//try
catch(SQLException ex){}
catch(Exception ex){}
//////////数据库///////////
}
try {
if(e.getSource()==readNext)////读取下一题///11111111
{ if(i<num) {
i=i+1;
try{
res.absolute(i);
strNumber=res.getString(1);
nameTest=res.getString(2);
}//try
catch(Exception ex){}
readNext.setText("读取下一题");
upAnswer.setEnabled(true);
lookScore.setEnabled(true);
String contentTest=strNumber+nameTest;//readTest.getTestContent();
testArea.setText(contentTest);
massegeArea.setText(null);
readNext.setEnabled(false);
try {
countTime.start();
}
catch(Exception event)
{
}
}
else
{ readNext.setEnabled(false);
upAnswer.setEnabled(false);
countTime.interrupt();
完成考试=true;
暂停或继续计时.setEnabled(false);
testArea.setText("恭喜你已完成考试!!!");
}
}///读取下一题
if(e.getSource()==upAnswer)//提交答案1111
{
readNext.setEnabled(true);
upAnswer.setEnabled(false);
//String answer="?";
for(int i=0;i<4;i++)
{
if(box[i].isSelected())
{
answer=box[i].getText();
box[i].setSelected(false);
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -