请大家帮帮忙!这是一个java考试时间的问题!

来源:互联网 发布:linux下修改ip 编辑:程序博客网 时间:2024/05/01 15:45

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class ClientTestArea extends Panel implements ActionListener,Runnable{
   static String verifyMess;
   static InetSocketAddress socketAddress;
   static File examinationFile;
   ArrayList<JRadioButton> choiceList;
   Socket socket;                              
   DataInputStream in;                          
   DataOutputStream out;
   Thread threadMessage=null;                        
   Thread thread;
   long time=0;
   Timer 计时器 = null;
   TextField 考试用时提示条=null;                         
   static JPanel examineArea;
   JLabel hintMess;                                  
   JButton handAnswer,startTest,lookScore;
   public ClientTestArea(){
      thread=new Thread(this);
   choiceList = new ArrayList<JRadioButton>();
      examineArea=new JPanel();
      examineArea.setForeground(Color.blue);    
      handAnswer=new JButton("提交答案");
      startTest=new JButton("显示试卷");
      lookScore=new JButton("查看分数");
   计时器 = new Timer(1000, this);
      考试用时提示条=new TextField("显示考试用时(倒计时)",28);
      考试用时提示条.setForeground(Color.red);
      考试用时提示条.setEditable(false);
      handAnswer.setEnabled(false);
      lookScore.setEnabled(false);
      hintMess=new JLabel("单击/"显示试卷/"按纽从服务器得到试卷");
      startTest.addActionListener(this);
      handAnswer.addActionListener(this);
      lookScore.addActionListener(this);  
      setLayout(new BorderLayout());
      JPanel pCenter=new JPanel();
      pCenter.setBackground(Color.yellow);
      pCenter.setLayout(new BorderLayout());
      pCenter.add(new JScrollPane(examineArea),BorderLayout.CENTER);
      add(pCenter,BorderLayout.CENTER);
      Panel p2=new Panel();
      p2.add(考试用时提示条);
      JPanel pSouth=new JPanel();
      pSouth.setBackground(Color.blue);
      pSouth.add(handAnswer);
      pSouth.add(startTest);
      pSouth.add(lookScore);
   pSouth.add(p2);
      add(pSouth,BorderLayout.SOUTH);
      JPanel pNorth=new JPanel();
      pNorth.setBackground(Color.green);
      pNorth.add(hintMess);
      add(pNorth,BorderLayout.NORTH);
   }
    public long getTime()
    {
      return time;
    }
    public void setSocketConnection(Socket socket,DataInputStream in,DataOutputStream out)
    {
      this.socket=socket;
      this.in=in;
      this.out=out;
      try{
           threadMessage.start();
         }
      catch(Exception e)
         {
         }
      if(this.socket!=null)
       {
   startTest.setEnabled(true);
   handAnswer.setEnabled(false);
   计时器.stop();
         考试用时提示条.setText("显示考试用时(倒计时)");
       }
    }
   public static void setVerifyMess(String mess){
      verifyMess=mess;
   }
   public static void setSocketAddress(InetSocketAddress address){
      socketAddress=address;
   }
   public static void setExaminationFile(File f){
      examinationFile=f;
      examineArea.removeAll();
   }
   public void actionPerformed(ActionEvent e){
    if (e.getSource() == 计时器)
       {
         time=time-1000;
         if(time<=0)                           
          {
    计时器.stop();
    handAnswer.setEnabled(false);
    handAnswer.setEnabled(false);
          }
          long  leftTime=time/1000;              
          long leftHour=leftTime/3600;          
          long leftMinute=(leftTime-leftHour*3600)/60;
          long leftSecond=leftTime%60;          
          考试用时提示条.setText("剩余时间:"+leftHour+"小时 "+leftMinute+"分 "+leftSecond+" 秒");
        }
      if(e.getSource()==startTest){
           choiceList.clear();
           lookScore.setEnabled(false);
           try{ makeConnection();
                examineArea.removeAll();
                out.writeUTF("显示试卷:"+verifyMess+"#"+examinationFile.getName());
                String m="读入的试题:"+examinationFile+"将刷新当前的试题,是否确认继续";
                int ok=JOptionPane.showConfirmDialog(this,m,"确认对话框",
                              JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
                if(ok==JOptionPane.YES_OPTION){
                  if(!(thread.isAlive())){
                    thread=new Thread(this);
 
                    thread.start();
                  }
                  handAnswer.setEnabled(true);
                }
          }
          catch(Exception exp){
                JOptionPane.showMessageDialog(this,"您未登录或选择试卷","操作提示",
                                              JOptionPane.PLAIN_MESSAGE);
          }
     }
     if(e.getSource()==handAnswer){
         String m="只有一次提交答案的机会!,一旦提交将无法继续答题,是否确认提交答案";
         int ok=JOptionPane.showConfirmDialog(this,m,"确认对话框",
                            JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
         if(ok==JOptionPane.YES_OPTION){
            StringBuffer str=new StringBuffer();
            for(int i=0;i<choiceList.size();i++){
               JRadioButton box=choiceList.get(i);
               if(box.isSelected())
                 str.append(box.getText());
            }
            String myAnswer=new String(str);
            try{ makeConnection();
                 out.writeUTF("提交的答案:"+verifyMess+"#"+myAnswer);
                 handAnswer.setEnabled(false);
                 lookScore.setEnabled(true);
            }
            catch(Exception exp){
                JOptionPane.showMessageDialog(this,"您未登录或选择试卷","操作提示",
                                              JOptionPane.PLAIN_MESSAGE);
            }
         }
     }
     if(e.getSource()==lookScore){
         StringBuffer str=new StringBuffer();
         for(int i=0;i<choiceList.size();i++){
            JRadioButton box=choiceList.get(i);
            if(box.isSelected())
             str.append(box.getText());
         }
         String myAnswer=new String(str);
         try{ makeConnection();
          out.writeUTF("查看得分:"+verifyMess+"#"+myAnswer+"*"+examinationFile.getName());
              if(!(thread.isAlive())){
                 thread=new Thread(this);
 
                 thread.start();
              }
         }
         catch(Exception exp){
              JOptionPane.showMessageDialog(this,"您未登录或选择试卷","操作提示",
                                            JOptionPane.PLAIN_MESSAGE);
         }
     } 
  }
  private void makeConnection() throws IOException {
       socket=new Socket();
       socket.connect(socketAddress);
       in=new DataInputStream(socket.getInputStream());
       out=new DataOutputStream(socket.getOutputStream());
  } 
  public void run(){ 
     while(true){
         String s=null;
         try
   {
            s=in.readUTF();
            if(s.startsWith("试卷内容:")){
                String content=s.substring(s.indexOf(":")+1);
                StringReader read=new StringReader(content);
                BufferedReader in= new BufferedReader(read);
                String str=null;
                int number=0;
                while((str=in.readLine())!=null){ //计算出题目总数
                   if(str.trim().startsWith("答题卡"))
                     number++;
                }
                examineArea.setLayout(new GridLayout(number,2));//第2列上放置答题卡
                JTextArea [] text=new JTextArea[number];
                for(int i=0;i<text.length;i++){
                   text[i]=new JTextArea(4,10);
                   text[i].setLineWrap(true);
                   text[i].setWrapStyleWord(true);
                   text[i].setFont(new Font("宋体",Font.BOLD,14));
                }
                in.close();
                out.close();
                read=new StringReader(content);
                in= new BufferedReader(read);
                int i=0;
                while((str=in.readLine())!=null){
                  if(!(str.trim().startsWith("答题卡")))
                     text[i].append(str+"/n");            //text[i]中显示第i题的内容
                  else{
                     examineArea.add(new JScrollPane(text[i]));
                     i++;
                     JRadioButton [] box=new JRadioButton[4];
                     ButtonGroup group=new ButtonGroup();
                     String [] choiceChar={"A","B","C","D"};
                     for(int k=0;k<4;k++){
                          box[k]=new JRadioButton(choiceChar[k],true);
                          group.add(box[k]);
                     }
                     JPanel pAddbox=new JPanel();
                     pAddbox.setBackground(Color.yellow);
                     pAddbox.setBorder(BorderFactory.createRaisedBevelBorder());
                     FlowLayout flow=new FlowLayout();
                     flow.setAlignment(FlowLayout.LEFT);
                     flow.setHgap(5);
                     pAddbox.setLayout(flow);
                     pAddbox.add(new JLabel("答题卡:"));
                     for(int k=0;k<4;k++){
                        pAddbox.add(box[k]);
                        choiceList.add(box[k]);
                     } 
                     examineArea.add(pAddbox);
                  }
                }
                examineArea.repaint();
                validate();
                in.close();
                out.close();
                hintMess.setText("祝考出好成绩");
                return;
            }
            if(s.startsWith("分数")){
               JOptionPane.showMessageDialog(this,s,"查询提示",JOptionPane.PLAIN_MESSAGE);
               return;
            }
   if (s.startsWith("考试用时:"))
   {
    String str = s.substring(s.indexOf(":") + 1);
    time = Long.parseLong(str);
    考试用时提示条.setText("" + time);
    计时器.start();
   }
   if (s.startsWith("考试结束:"))
   {
    计时器.stop();
   }

   Thread.sleep(5);           
         }
         catch(Exception e){
            JOptionPane.showMessageDialog(this,""+e,"异常提示",JOptionPane.PLAIN_MESSAGE);
            return;
         }
     }
  }
}

 

这个程序现在有个问题,就是

import javax.swing.*;和import java.util.*;现在对时间这一块起冲突,运行的结果是

 

 

E:/aa>javac ClientWindow.java
./ClientTestArea.java:18: 对 Timer 的引用不明确,java.util 中的 类 java.util.Tim
er 和 javax.swing 中的 类 javax.swing.Timer 都匹配
   Timer 计时器 = null;
   ^
./ClientTestArea.java:31: 对 Timer 的引用不明确,java.util 中的 类 java.util.Tim
er 和 javax.swing 中的 类 javax.swing.Timer 都匹配
          计时器 = new Timer(1000, this);
                    ^
2 错误

 

 

但是当我把//import java.util.*;注释掉以后运行的结果提示我说:

E:/aa>javac ClientWindow.java
./ClientTestArea.java:11: 找不到符号
符号: 类 ArrayList
位置: 类 ClientTestArea
   ArrayList<JRadioButton> choiceList;
   ^
./ClientTestArea.java:25: 找不到符号
符号: 类 ArrayList
位置: 类 ClientTestArea
          choiceList = new ArrayList<JRadioButton>();
                           ^
2 错误

想请明白的哥哥姐姐叔叔阿姨小弟小妹们看看,这个程序怎样才能叫他运行的时候不出错,谢谢

原创粉丝点击