⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kaoshi.java

📁 关于考试系统的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
 
ChoiceFile.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChoiceFile extends Panel implements ItemListener,ActionListener,Runnable
{
   Button 得到试题文件=null;
   Choice list=null; 
   Socket  socket=null;
   DataInputStream in=null; 
   DataOutputStream out=null; 
   Thread thread=null;
   boolean success=false, isSelected=false;
   int n=0;
   
   public ChoiceFile()
    {
      得到试题文件=new Button("将试题文件添加到下拉列表中");
      得到试题文件.setEnabled(false);
      得到试题文件.addActionListener(this);
      list=new Choice();
      list.add("**********");
      add(得到试题文件);
      add(list);
      list.addItemListener(this);
      thread=new Thread(this);
    }
  public void setSocketConnection(Socket socket,DataInputStream in,DataOutputStream out)
    {
      this.socket=socket;
      this.in=in;
      this.out=out; 
      try {
            thread.start(); 
          }
      catch(Exception ee)
          {
          }
      if(this.socket!=null)
       {
         得到试题文件.setEnabled(true);
       } 
    }
   public void actionPerformed(ActionEvent e)
    {
         try 
           { 
             out.writeUTF("列出考试文件");
           }
           catch(IOException ee)
           { 
             success=false;
           }
       
    } 
  public void itemStateChanged(ItemEvent e)
    { 
      n=list.getItemCount();
      if(n>=1&&isSelected==true)
        {
          String listItem=list.getSelectedItem();
           try
              {
                out.writeUTF("考试文件的名字:"+listItem);
                out.writeUTF("请通知考试界面考试用时:");
              }
           catch(IOException ee)
              {
              }
        }         
   }
  public String 得到选择的文件名()
   {
    return list.getSelectedItem();
   }
  public void run()
   {
    String s=null;
    list.removeAll();
    while(true)
      { 
        try
           {
             s=in.readUTF();          
              if(s.startsWith("考试文件:"))
                 {
                   String listItem=s.substring(s.indexOf(":")+1);
                   list.add(listItem);
                   isSelected=true;
                 }
               if(s.startsWith("已经全部列出:"))
                 {
                   success=true; 
                   break;
                 }
               
           }
        catch (Exception ee) 
           {
           }   
      }
 }
 public boolean getSuccess()
 { 
   n=list.getItemCount();
   if(n>0&&success==true)
      return true;
   else
      return false;
 }
}

 

ReadTestquestion.java


import java.io.*;
import java.util.*;
public class ReadTestquestion
{  String filename="",         
   correctAnswer="",           
   testContent="" ,            
   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;
        }
   
}

ClientBS.java


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.applet.*;
public class ClientBS extends Applet implements ActionListener
{
  Button     开始测试=null;
  Label      提示条1=null,提示条2=null;
  Socket  socket=null;
  DataInputStream in=null; 
  DataOutputStream out=null; 
  ChoiceFile 选择试题文件界面=null;
  ClientTestArea 考试界面=null;
  Panel west, center;
 
  public void init() 
   {
    setLayout(new BorderLayout()); 
    开始测试    =new Button("开始考试");
    开始测试.addActionListener(this);
    选择试题文件界面=new ChoiceFile();
    选择试题文件界面.setVisible(false);
    考试界面=new ClientTestArea();
    考试界面.setVisible(false);
    提示条1=new Label("正在连接到服务器,请稍等...");
    提示条2=new Label("选择一个文件后,单击"+开始测试.getLabel()+"按钮");
    提示条2.setVisible(false);
    Panel H1= new Panel();        
    H1.add(提示条1);
    Panel H2= new Panel(); 
    H2.add(提示条2);
    Panel H3= new Panel(); 
    H3.add(选择试题文件界面);
    west=new Panel();
    center=new Panel();
    west.setLayout(new GridLayout(3,1));
    west.add(H1);
    west.add(H2);
    west.add(H3);
    center.setLayout(new BorderLayout());
    center.add(开始测试,BorderLayout.NORTH);
    center.add(考试界面,BorderLayout.CENTER);
    add(west,BorderLayout.WEST);
    add(center,BorderLayout.CENTER);
    setVisible(true);
    setBounds(60,40,660,460);
    validate();
   }
 public void start()
   {
     if(socket!=null&&in!=null&&out!=null)
        {  try
              {
                socket.close();
                in.close(); 
                out.close();
                考试界面.setVisible(false);
              }
          catch(Exception ee)
              {
               
              }
        }
     try
        {
         socket = new Socket(this.getCodeBase().getHost(), 6666);
         in=new DataInputStream(socket.getInputStream());
         out=new DataOutputStream(socket.getOutputStream());
        }
     catch (IOException ee)
        { 
          提示条1.setText("连接失败");
        }
     if(socket!=null)
        {
         InetAddress address=socket.getInetAddress();
         提示条1.setEnabled(true);
         提示条1.setText("您连接:"+address+"成功");
         选择试题文件界面.setVisible(true);
         选择试题文件界面.validate();
         选择试题文件界面.setSocketConnection(socket,in,out);
         提示条2.setVisible(true);
         west.validate();
         validate();
        } 
   } 
  
 public void stop()
   {
     try 
         {
           socket.close();
         }
     catch(IOException e)
         {
          this.showStatus(e.toString());
         }
   }
 public void actionPerformed(ActionEvent e)
 {  
    if(e.getSource()==开始测试)
      {
         if(选择试题文件界面.getSuccess())
             { 
               考试界面.setSocketConnection(socket,in,out);
               考试界面.setVisible(true);
               考试界面.set试题显示区("你选择了试题:"+选择试题文件界面.得到选择的文件名());
               center.validate();
               validate();
             }
       
      }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -