?? clienttestarea.java
字號:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package client;/** * * @author Administrator */import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.*;import java.net.*;public class ClientTestArea extends Panel implements ActionListener,Runnable{ Socket socket=null; DataInputStream in=null; DataOutputStream out=null; Thread threadMessage=null; TextArea 試題顯示區=null,答案顯示區=null; Checkbox box[]; String answer="?"; long time=0; Timer 計時器=null; Button 提交該題答案,讀取下一題,查看得分; TextField 考試用時提示條=null; public ClientTestArea() { this.socket=socket; this.in=in; this.out=out; threadMessage=new Thread(this); 試題顯示區=new TextArea("",16,5,TextArea.SCROLLBARS_BOTH); 答案顯示區=new TextArea("",3,5,TextArea.SCROLLBARS_VERTICAL_ONLY); 答案顯示區.setFont(new Font("TimesRoman",Font.PLAIN,14)); 試題顯示區.setFont(new Font("TimesRoman",Font.PLAIN,14)); 試題顯示區.setForeground(Color.blue); 試題顯示區.setText(null); 答案顯示區.setForeground(new Color(255,100,100)); 答案顯示區.setText("單擊查看得分按鈕,可以在這里看到得分和正確答案"); 計時器=new Timer(1000,this); String s[]={"A","B","C","D"}; box=new Checkbox[4]; for(int i=0;i<4;i++) { box[i]=new Checkbox(s[i]); } 提交該題答案=new Button("提交該題答案"); 讀取下一題=new Button("讀取下一題"); 查看得分=new Button("查看得分"); 考試用時提示條=new TextField("顯示考試用時(倒計時)",28); 考試用時提示條.setForeground(Color.red); 考試用時提示條.setEditable(false); 讀取下一題.setEnabled(true); 提交該題答案.setEnabled(false); 讀取下一題.setEnabled(false); 讀取下一題.addActionListener(this); 提交該題答案.addActionListener(this); 查看得分.addActionListener(this); Panel pAddbox=new Panel(); for(int i=0;i<4;i++) { pAddbox.add(box[i]); } setLayout(new BorderLayout()); Panel pCenter=new Panel(); pCenter.setLayout(new BorderLayout()); pCenter.add(試題顯示區,BorderLayout.CENTER); pCenter.add(答案顯示區,BorderLayout.NORTH); add(pCenter,BorderLayout.CENTER); Panel p1=new Panel(); p1.add(pAddbox); p1.add(提交該題答案); p1.add(讀取下一題); p1.add(查看得分); Panel p2=new Panel(); p2.add(考試用時提示條); Panel pSouth=new Panel(); pSouth.setLayout(new GridLayout(2,1)); pSouth.add(p1); pSouth.add(p2); add(pSouth,BorderLayout.SOUTH); } 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) { 讀取下一題.setEnabled(true); 提交該題答案.setEnabled(false); 計時器.stop(); 考試用時提示條.setText("顯示考試用時(倒計時)"); } }public void set試題顯示區(String s) { 試題顯示區.setText(s); }public void actionPerformed(ActionEvent e) { if(e.getSource()==計時器) { time=time-1000; if(time<=0) { 計時器.stop(); 讀取下一題.setEnabled(false); 提交該題答案.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()==讀取下一題) { 讀取下一題.setLabel("讀取下一題"); 提交該題答案.setEnabled(true); 讀取下一題.setEnabled(false); try { out.writeUTF("讀取下一題"); } catch(Exception event) { } } if(e.getSource()==提交該題答案) { 讀取下一題.setEnabled(true); 提交該題答案.setEnabled(false); for(int i=0;i<4;i++) { if(box[i].getState()==true) { answer=box[i].getLabel(); box[i].setState(false); break; } } try { out.writeUTF("提交的答案:"+answer); } catch(IOException ee) { } } if(e.getSource()==查看得分) { try { out.writeUTF("查看得分"); } catch(IOException ee) { } } }public void run() { while(true) { String s=null; try { s=in.readUTF(); if(s.startsWith("試題內容:")) { String content=s.substring(s.indexOf(":")+1); 試題顯示區.setText(s); } if(s.startsWith("分數")) { 答案顯示區.setText("\n"+s); } 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) { 試題顯示區.setText("和服務器的連接斷開"); } } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -