📄 usertest.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package testgui;import DBOperation.DBItem;import java.awt.*;import java.awt.event.*;import java.util.Random;import javax.swing.*;/** * * @author RJ-408 */public class UserTest extends JPanel implements ActionListener,ItemListener,Runnable { JMenuBar menubar; JMenu 试题,系统,用户; JMenuItem 关于,设置,插入题目,提交答案,删除题目,添加用户,删除用户,用户权限,添加试题,退出系统; AddUser addUser; Setting set=new Setting(this);; DelUser delUser; UserPower power; AddItem additem; String s[][]=new String [30][30]; int i=0,n=0; int []score=new int[30];// String anserstr[]={"A","B","C","D","Y","N"}; Choice list=null; JTextArea 试题显示区=null,消息区=null,填空题答案=null; JCheckBox box[]; //for(int i=0;;i++) // JRadioButton answer=new JRadioButton(anserstr[i]); JButton 提交该题答案,读取下一题,查看分数1; JLabel welcomeLabel=null; Thread countTime=null;//线程 long time=0; JTextField timeShow=null; boolean 是否关闭计时器=false, 是否暂停计时=false; JButton 暂停或继续计时=null; public UserTest() { super(); list= new Choice(); list.add(""); list.add("选择题"); list.add("填空题"); list.add("判断题"); list.add("简单题"); list.add("中等题"); list.add("困难题"); //String fileName[]=dir.list(fileTxt);//获得文件名 // for(int i=0;i<fileName.length;i++) // { // list.add(fileName[i]);//将文件名加入到列表框中 // } menubar=new JMenuBar(); 试题=new JMenu("试题"); 系统=new JMenu("系统"); 用户=new JMenu("用户"); 关于=new JMenuItem("关于"); 设置=new JMenuItem("设置"); 插入题目=new JMenuItem("插入题目"); 提交答案=new JMenuItem("提交答案"); 删除题目=new JMenuItem("删除题目"); 添加用户=new JMenuItem("添加用户"); 删除用户=new JMenuItem("删除用户"); 用户权限=new JMenuItem("用户权限"); 添加试题=new JMenuItem("添加试题"); 退出系统=new JMenuItem("退出系统"); menubar.add(试题); menubar.add(用户); menubar.add(系统); 试题.add(插入题目); 试题.add(删除题目); 试题.add(提交答案); 用户.add(添加用户); 用户.add(删除用户); 用户.add(用户权限); 系统.add(设置); 系统.add(退出系统); 系统.add(关于); 试题.addActionListener(this); 系统.addActionListener(this); 用户.addActionListener(this); 关于.addActionListener(this); 设置.addActionListener(this); 插入题目.addActionListener(this); 提交答案.addActionListener(this); 删除题目.addActionListener(this); 添加用户.addActionListener(this); 删除用户.addActionListener(this); 用户权限.addActionListener(this); 退出系统.addActionListener(this); 试题显示区=new JTextArea(8,8); 试题显示区.setLineWrap(true); 试题显示区.setWrapStyleWord(true); 试题显示区.setFont(new Font("TimesRoman",Font.PLAIN,14)); 试题显示区.setForeground(Color.blue); 填空题答案=new JTextArea(2,2); 填空题答案.setLineWrap(true); 填空题答案.setWrapStyleWord(true); 填空题答案.setFont(new Font("TimesRoman",Font.PLAIN,14)); 填空题答案.setForeground(Color.blue); 消息区=new JTextArea(8,8); 消息区.setForeground(Color.blue); 消息区.setLineWrap(true); 消息区.setWrapStyleWord(true); String s[]={"A","B","C","D","Y","N"}; box=new JCheckBox[6]; for(int i=0;i<6;i++) { box[i]=new JCheckBox(s[i]) ; } countTime=new Thread(this); 暂停或继续计时=new JButton("暂停计时"); 暂停或继续计时.addActionListener(this); 提交该题答案=new JButton("提交该题答案"); 读取下一题=new JButton("读取第一题"); 查看分数1=new JButton("查看分数"); 读取下一题.setForeground(Color.blue); 提交该题答案.setForeground(Color.blue); 查看分数1.setForeground(Color.blue); 提交该题答案.setEnabled(false); 提交该题答案.addActionListener(this); 读取下一题.addActionListener(this); 查看分数1.addActionListener(this); list.addItemListener(this); JPanel pAddbox=new JPanel(); for(int a=0;a<6;a++) { pAddbox.add(box[a]); } Box boxH1=Box.createVerticalBox(), boxH2=Box.createVerticalBox(), baseBox=Box.createHorizontalBox(); boxH1.add(new JLabel("备注")); boxH1.add(new JScrollPane(消息区)); boxH1.add(new JLabel("选择试题文件")); boxH1.add(list); boxH1.add(查看分数1); 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(试题显示区)); 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); add(menubar,BorderLayout.NORTH); welcomeLabel=new JLabel("欢迎考试,提高水平,不能作弊",JLabel.CENTER); welcomeLabel.setFont(new Font("隶书",Font.PLAIN,24)); welcomeLabel.setForeground(Color.blue); this.setVisible(false); this.setBounds(150, 150, 1024, 460); } public void itemStateChanged(ItemEvent e) { timeShow.setText(null); 是否关闭计时器=false; 是否暂停计时=false; 暂停或继续计时.setText("暂停计时"); String name=list.getSelectedItem(); // 打开相应的试题数据库 DBItem item=new DBItem(); if("选择题".equals(name)) { s=item.getItemL("ItemTypeID", 1); time=0; /* i=0; n=0; score=null;*/ } if("判断题".equals(name)) { s=item.getItemL("ItemTypeID", 2); time=0; /* i=0; n=0; score=null;*/ } if("填空题".equals(name)) { s=item.getItemL("ItemTypeID", 3); time=0; /* i=0; n=0; score=null;*/ } if("简单题".equals(name)) { s=item.getItemL("ItemDifID", 1); time=0; /* i=0; n=0; score=null;*/ } if("中等题".equals(name)) { s=item.getItemL("ItemDifID", 2); time=0; /* i=0; n=0; score=null;*/ } if("困难题".equals(name)) { s=item.getItemL("ItemDifID", 3); time=0; /* i=0; n=0; score=null;*/ } if((String)list.getSelectedItem()==null) { s=item.getItemL(null, 0); time=0; /* i=0; n=0; score=null;*/ } if(countTime.isAlive()) { 是否关闭计时器=true; countTime.interrupt(); } countTime=new Thread(this); 消息区.setText(null); 试题显示区.setText(null); 读取下一题.setText("读取第一题"); 提交该题答案.setEnabled(false); 读取下一题.setEnabled(true); } public void actionPerformed(ActionEvent e) { if(e.getSource()==读取下一题) { // 打开相应的试题数据库 String str=s[i][3]; // 试题显示区.setText(contentTest); 读取下一题.setText("读取下一题"); 试题显示区.setText(str); time=Integer.parseInt(s[i][6]); //time=120000; 提交该题答案.setEnabled(true); 消息区.setText(null); 读取下一题.setEnabled(false); if(set.timeselct()) { if(countTime.isAlive()) { 是否关闭计时器=true; countTime.interrupt(); } else{ try { countTime.start(); } catch(Exception event) { } } } if(i<s.length){ i++; }else{ 读取下一题.setEnabled(false); } } if(e.getSource()==提交该题答案) { 读取下一题.setEnabled(true); 提交该题答案.setEnabled(false); String answer=填空题答案.getText(); 填空题答案.setText(""); for(int a=0;a<6;a++) { if(box[a].isSelected()) { answer=box[a].getText(); box[a].setSelected(false); break; } } if(answer.equals(s[i-1][4])) { score[i-1]=Integer.parseInt(s[i-1][5]); n++; } //System.out.println(score[i-1]); } if(e.getSource()==查看分数1) { int b=0; for(int a=0;a<score.length;a++) { b =b+score[a]; } 消息区.setText("您目前已完成的 "+i+" 个题目. 共答对了"+n+"道题目,您的分数是"+b); // 消息区.setText("分数:"+score+"\n"+messages); } if(e.getSource()==暂停或继续计时) { if(是否暂停计时==false) { 暂停或继续计时.setText("继续计时"); 是否暂停计时=true; } else if(是否暂停计时==true) { 暂停或继续计时.setText("暂停计时"); 是否暂停计时=false; countTime.interrupt(); } }/***************************************************************************/ if(e.getSource()==插入题目) { additem=new AddItem(this); additem.setVisible(true); } if(e.getSource()==删除题目) { delete del=new delete(this); del.setVisible(true); } if(e.getSource()==提交答案) { 读取下一题.setEnabled(true); 提交该题答案.setEnabled(false); String answer=填空题答案.getText(); 填空题答案.setText(""); for(int a=0;a<6;a++) { if(box[a].isSelected()) { answer=box[a].getText(); box[a].setSelected(false); break; } } if(answer.equals(s[i-1][4])) { score[i-1]=Integer.parseInt(s[i-1][5]); n++; } }/***************************************************************************/ if(e.getSource()==用户权限) { power=new UserPower(this); power.setVisible(true); } if(e.getSource()==删除用户) { delUser=new DelUser(this); delUser.setVisible(true); } if(e.getSource()==添加用户) { addUser=new AddUser(); addUser.setVisible(true); } /**************************************************************************/ //以下是关于系统菜单的事件响应 if(e.getSource()==退出系统) System.exit(0); if(e.getSource()==设置) { set.setVisible(true); s=set.settingItem(); } if(e.getSource()==关于) { String About="标准考试系统 1.0.0 作者:Dream.Java "; JOptionPane.showMessageDialog(this,About,"警告",JOptionPane.WARNING_MESSAGE); } } /**************************************************************************/ public synchronized void run() { while (true) { if (time < 1) { 是否关闭计时器 = true; countTime.interrupt(); 提交该题答案.setEnabled(false); 读取下一题.setEnabled(false); timeShow.setText("用时尽,考试结束"); } 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 ee) { if (是否关闭计时器 == true) { return; } } while (是否暂停计时 == true) { try { wait(); } catch (InterruptedException ee) { if (是否暂停计时 == false) { notifyAll(); } } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -