?? 附錄b例子.txt
字號(hào):
(1) 客戶端程序:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//輸入考號(hào)的文本框,通過鍵盤事件限制考生只能輸入數(shù)字:
class 考號(hào)文本框 extends TextField implements KeyListener
{考號(hào)文本框(int n)
{super(n); //調(diào)用父類的構(gòu)造方法確定文本框的長(zhǎng)度。
this.addKeyListener(this); //增加鍵盤監(jiān)視器。
}
public void keyPressed(KeyEvent e)//通過實(shí)現(xiàn)KeyListener接口限制客戶的輸入//范圍。
{ int c=e.getKeyCode();
boolean b1=(c<'0'||c>'9'),
b2=(c!='.'),
b3=(c!=KeyEvent.VK_BACK_SPACE);
if(b1&&b2&&b3) //判斷是否輸入了非數(shù)字字符。
{String temp=
this.getText().substring(0,this.getText().indexOf(c));
this.setText(temp);
}
}
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
}
/*答題卡,一個(gè)答題卡中有A、B、C、D供選擇使用,并通過ItemEvent事件
記錄考生的選擇。
*/
class 答題卡 extends Panel implements ItemListener
{ Checkbox box_a,box_b,box_c,box_d; // 代表答題卡中A、B、C、D選項(xiàng)的對(duì)象。
Label label;
int number; //答題卡中的試題題號(hào)。
CheckboxGroup group; //答題為單項(xiàng)選擇題。
String answer="?"; //記錄被選擇的答案。
答題卡()
{ number=0;
group=new CheckboxGroup();
box_a=new Checkbox("A",false,group);
box_b=new Checkbox("B",false,group);
box_c=new Checkbox("C",false,group);
box_d=new Checkbox("D",false,group);
box_a.addItemListener(this);
box_b.addItemListener(this);
box_c.addItemListener(this);
box_d.addItemListener(this);
label=new Label("("+number+")");
add(label); add(box_a); add(box_b);
add(box_c); add(box_d);
}
//通過實(shí)現(xiàn)ItemListener接口獲取考生的選擇:
public void itemStateChanged(ItemEvent e)
{ Checkbox box=(Checkbox)e.getItemSelectable();//獲取事件源。
answer=box.getLabel(); //獲取選項(xiàng)的名字。
box.setFont(new Font("Courier",Font.BOLD,18));
box.setForeground(Color.red); //用特殊顏色和字體顯示被選擇的答案。
if(box_a!=box) //未被選中的保持原來的顏色和字體。
{box_a.setFont(
new Font("TimesRoman",Font.PLAIN,12));
box_a.setForeground(Color.black);
}
if(box_b!=box){box_b.setFont(
new Font("TimesRoman",Font.PLAIN,12));
box_b.setForeground(Color.black);
}
if(box_c!=box){box_c.setFont(
new Font("TimesRoman",Font.PLAIN,12));
box_c.setForeground(Color.black);
}
if(box_d!=box){box_d.setFont(
new Font("TimesRoman",Font.PLAIN,12));
box_d.setForeground(Color.black);
}
}
}
//主類,實(shí)現(xiàn)考試界面
public class ExaminationClient extends Applet
implements Runnable,ActionListener
{ TextField 姓名=new TextField(10); //輸入姓名的文本框。
考號(hào)文本框 考號(hào)=new 考號(hào)文本框(18); //輸入考號(hào)的文本框。
TextArea 試題區(qū)=new TextArea(); //試題區(qū)。
Socket socket=null; //和服務(wù)器建立連接的網(wǎng)絡(luò)套接字。
DataInputStream in=null; //通過套接字連接讀取服務(wù)器發(fā)送來的試題。
DataOutputStream out=null; //通過套接字提交給服務(wù)器考號(hào)、姓名、和答卷。
Button 開始考試=new Button("開始考試"),
提交答卷=new Button("提交答卷");
Thread thread=null; //通過一個(gè)單獨(dú)的線程讀取試題。
答題卡 題卡[]=new 答題卡[21]; //制作答題卡用的數(shù)組。
Choice choice=null;int N=0;
public void init()
{ setLayout(null);
choice=new Choice(); //選擇一套考題。
N=Integer.parseInt(getParameter("總數(shù)"));
for(int i=1;i<=N;i++)//從web頁上獲取考題的名字。
}
Panel p=new Panel(); //制作答題卡用的面板。
p.setLayout(new GridLayout(22,1)); //根據(jù)試題的要求制作答題卡。
p.add(new Label("(一)選擇填空題(共10小題):"));
for(int i=1;i<=10;i++)
{ 題卡[i]=new 答題卡();
題卡[i].label.setText("("+i+")");
p.add(題卡[i]);
}
p.add(new Label("(二)閱讀理解:"));
for(int i=11;i<=15;i++)
{ 題卡[i]=new 答題卡();
int m=i-10;
題卡[i].label.setText("("+m+")");
p.add(題卡[i]);
}
ScrollPane scroll=new ScrollPane();
scroll.add(p); //將含有答題卡的面板放入一個(gè)滾動(dòng)窗格中。
add(scroll);
add(試題區(qū));
int height,width;
height=getSize().height; width=getSize().width;
//安排各個(gè)組件的位置:
scroll.setBounds(0,32,2*width/5,height-65);
試題區(qū).setBounds(2*width/5+2,32,3*width/5-10,height-65);
試題區(qū).setEditable(false);
Panel north=new Panel();
Label L1=new Label("輸入姓名:",Label.CENTER);
Label L2=new Label("輸入考號(hào):",Label.CENTER);
north.add(L1);
north.add(姓名);
north.add(L2);
north.add(考號(hào));
north.add(new Label("選擇一套試題:"));
north.add(choice);
north.add(開始考試);
add(north);
north.setBounds(20,2,width-25,30);
Panel south=new Panel();
south.add(提交答卷);
add(south);
south.setBounds(2*width/5+2,height-35,3*width/5-4,30);
開始考試.addActionListener(this); //通過按扭事件通知服務(wù)器考試開始。
提交答卷.addActionListener(this); //通過按扭事件提交答卷。
}
public void start()
{
if (thread==null)
{thread=new Thread(this); //創(chuàng)建用于讀取服務(wù)器信息的線程。
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void run()
{String s=null;
try
{socket=new Socket(this.getCodeBase().getHost(), 6331);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch (IOException e){}
while(true) //讀取服務(wù)器發(fā)來的試題。
{ try
{s=in.readUTF();
if(s!=null)
{試題區(qū).append(s+"\n");
}
}
catch (IOException e)
{姓名.setText("已經(jīng)和服務(wù)器斷開");
break;
}
}
}
public void actionPerformed(ActionEvent e) //通過該按扭事件通知考試開始,
//以便服務(wù)器發(fā)送試題給考生。
{if (e.getSource()==開始考試)
{ //驗(yàn)證是否輸入了姓名和考號(hào):
String name1="必須輸入姓名";
String kaohao1="必須輸入考號(hào)";
name1=姓名.getText()+name1;
kaohao1=考號(hào).getText()+kaohao1;
if (name1.startsWith("必須輸入姓名")||
kaohao1.startsWith("必須輸入考號(hào)"))
{姓名.setText("必須輸入姓名");
考號(hào).setText("必須輸入考號(hào)");
}
else{
try{//將所選的試題題目發(fā)給服務(wù)器:
out.writeUTF("start:"+choice.getSelectedItem());
}
catch(IOException e1)
{
}
}
}
if (e.getSource()==提交答卷) //通過該按扭事件提交答卷。
{ StringBuffer s=new StringBuffer();
for(int i=1;i<=15;i++) //獲取考生的答案。
{ s.append(題卡[i].answer);
}
try
{ //將答卷、姓名、考號(hào)按著一定格式發(fā)送給服務(wù)器:
out.writeUTF(new String(s)+
"#"+姓名.getText()+考號(hào).getText()+"###");
}
catch(IOException e1){}
開始考試.setEnabled(false);
提交答卷.setEnabled(false);
}
}
}
(2)服務(wù)器端程序
import java.io.*;import java.net.*;
import java.util.*;
public class ExaminationServer
{ public static void main(String args[])
{ServerSocket server=null;
Socket you=null;
while(true)
{try
{ server=new ServerSocket(6331);
}
catch(IOException e1)
{System.out.println("正在監(jiān)聽");}
try {
you=server.accept(); //接收客戶的套接字。
}
catch (IOException e)
{System.out.println("正在等待客戶");
}
if(you!=null)
{
new Server_thread(you).start(); //為客戶服務(wù)的線程開始工作。
}
else {
continue;
}
}
}
}
class Server_thread extends Thread
{ Socket socket=null;
File file=null;
DataOutputStream out=null;
DataInputStream in=null;int n=0;
String huida=null;
PrintStream file_out=null;
OutputStream out1=null;
String buffer=null;
byte b[]=new byte[100];
String 答案=null;
int 得分=0;
String 考試者=null;
Server_thread(Socket t)
{ socket=t;
try {in=new DataInputStream(socket.getInputStream());
out1=socket.getOutputStream();
out=new DataOutputStream(out1);
}
catch (IOException e)
{
}
}
public void run()
{ String s=null,s1=null;
while(true)
{
try{
s=in.readUTF(); //讀取客戶發(fā)來的信息。
if(s.startsWith("start:")) //如果收到的開始考試信息。
{//獲取考生所選的試題題目:
String str=s.substring(s.indexOf(":")+1);
//建立到試題文件的輸入流:
BufferedReader file_in=
new BufferedReader(new FileReader(str));
try {//首先讀取試題文件的第一行,因?yàn)榈谝恍惺菢?biāo)準(zhǔn)答案。
答案=file_in.readLine();
答案=答案.trim();
//然后將試題的其余部分發(fā)送給考生客戶:
while( (s1=file_in.readLine())!=null)
{out.writeUTF(s1);
}
file_in.close();
}
catch (IOException e)
{
}
}
if(s.endsWith("#")) //獲取考生提交的答卷,并評(píng)閱答卷:
{得分=0;
if(答案!=null)
{int n=s.indexOf("#"); //提交的答卷的結(jié)束位置
int m=s.indexOf("###");//姓名和考號(hào)的結(jié)束位置。
for(int i=0;i<10;i++)
if(s.charAt(i)==答案.charAt(i))
{ 得分=得分+2; }
for(int i=10;i<=n-1;i++)
if(s.charAt(i)==答案.charAt(i))
{得分=得分+4; }
考試者=s.substring(n+1,m); //獲取考生的姓名和考號(hào)
//在當(dāng)前目錄下創(chuàng)建一個(gè)子目錄:Student,用來存放各個(gè)考生的成績(jī):
File dir=new File("Student");
dir.mkdir();
//根據(jù)考生的姓名、考號(hào)創(chuàng)建一個(gè)文件對(duì)象
file=new File(dir,考試者+".txt");
file_out=new PrintStream(
new FileOutputStream(file));
//將考生的姓名、考號(hào)和成績(jī)寫入文件:
file_out.println("考試者:"+考試者);
file_out.println("你的分?jǐn)?shù):"+得分);
file_out.println("你的答題:"+s.substring(0,n));
file_out.println("標(biāo)準(zhǔn)答案:"+答案);
}
}
sleep(30);
}
catch(InterruptedException e)
{
}
catch (IOException e)
{
System.out.println("客戶離開");
try{out.close();in.close();
}
catch (IOException e1){}
break;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -