正文

JAVA C/S 考试模拟系统源代码(客户端)2006-08-24 22:16:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/edwardguo/17870.html

分享到:

 

TestPanel.java

package ClientPart;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;

public class TestPanel extends JPanel implements ActionListener,Runnable{
 private Socket connectToServer;
 private DataInputStream inFromServer;
 private DataOutputStream outToServer;
 private Thread thread;
 
 private Timer testTimer;
 private int testTime;
 
 private JButton startButton;
 private JLabel timeLabel;
 private JTextArea questionArea;
 private Checkbox radioButton[] = new Checkbox[4];
 private CheckboxGroup buttonGroup = new CheckboxGroup();
 private JButton answerButton;
 private JButton questionButton;
 private JButton scoreButton;
 public TestPanel(InetAddress add,int port){
  initPanelGUI();
  try{
   connectToServer = new Socket(add,port);
   inFromServer = new DataInputStream(
     connectToServer.getInputStream());
   outToServer = new DataOutputStream(
     connectToServer.getOutputStream());
   System.out.println(connectToServer.getInetAddress());
     
   }
  catch(IOException e){
   System.out.println("TestPanel连接错误");
   startButton.setEnabled(false);
  }
  testTimer = new Timer(1000,this);
  thread = new Thread(this);
  thread.start();
 }
 
 public void actionPerformed(ActionEvent e){
  if(e.getSource()==startButton){
   startButtonPerformed();
  }
  if(e.getSource()==testTimer){
   testTimerPerformed();
  }
  if(e.getSource()==questionButton){
   questionButtonPerformed();
  }
  if(e.getSource()==answerButton){
   answerButtonPerformed();
  }
  if(e.getSource()==scoreButton){
   scoreButtonPerformed();
  }  
 }
 public void run(){
  String inStr ="";
  while(true){
   try{
    inStr = inFromServer.readUTF();
    if(inStr.startsWith("考试时间")){
     inStr = inStr.substring(inStr.indexOf("@")+1);
     testTime = Integer.parseInt(inStr);
     timeLabel.setText(convertTime(testTime));
     testTimer.start();
    }
    if(inStr.startsWith("下一题")){
     inStr = inStr.substring(inStr.indexOf("@")+1);
     questionArea.setText(inStr);
     if(inStr.startsWith("试题结束")){
      testTimer.stop();
      questionButton.setEnabled(false);
      answerButton.setEnabled(false);
      scoreButton.setEnabled(true);
     }
    }
    if(inStr.startsWith("成绩")){
     JOptionPane.showMessageDialog(null,inStr,"成绩显示",JOptionPane.INFORMATION_MESSAGE);
     socketClosing();
    }
   }
   catch(Exception e){
    socketClosing();
    questionArea.setText("服务器连接终止");
    break;
   }
  }
 }
 
 private void initPanelGUI(){
  setLayout(new BorderLayout());
  JPanel northPanel = new JPanel();
  northPanel.setLayout(new GridLayout(2,1));
  startButton = new JButton("开始考试");
  startButton.addActionListener(this);
  timeLabel = new JLabel("考试剩余时间");
  northPanel.add(startButton);
  northPanel.add(timeLabel);
  add(northPanel,BorderLayout.NORTH);
  questionArea = new JTextArea(30,10);
  questionArea.setLineWrap(true);
  questionArea.setFont(new Font("幼圆",Font.PLAIN,16));
  int vScroll = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
  int hScroll = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
  add(new JScrollPane(questionArea,vScroll,hScroll),BorderLayout.CENTER);
  JPanel southPanel = new JPanel();
  JPanel radioPanel = new JPanel();
  String s[] = {"A","B","C","D"};
  for(int i=0;i<radioButton.length;i++){
   radioButton[i] = new Checkbox(s[i],buttonGroup,false);
   radioPanel.add(radioButton[i]);
  }
  answerButton = new JButton("提交答案");
  answerButton.setEnabled(false);
  answerButton.addActionListener(this);
  questionButton = new JButton("下一题");
  answerButton.setEnabled(false);
  questionButton.addActionListener(this);
  scoreButton = new JButton("成绩");
  scoreButton.setEnabled(false);
  scoreButton.addActionListener(this);
  southPanel.add(radioPanel);
  southPanel.add(answerButton);
  southPanel.add(questionButton);
  southPanel.add(scoreButton);
  add(southPanel,BorderLayout.SOUTH);
 }
 
 private String convertTime(int time){
  int leftTime = time/1000;
  int leftHour = leftTime/3600;
  int leftMinute = (leftTime-leftHour*3600)/60;
  int leftSecond = leftTime%60;
  String timeStr = "剩余时间:"+leftHour+"小时 "+leftMinute+"分 "+leftSecond+"秒 ";
  return timeStr;
 }
 
 
 public void startButtonPerformed(){
  startButton.setEnabled(false);
  questionButton.setEnabled(true);
  try{
   outToServer.writeUTF("开始考试");
  }
  catch(IOException ioe){
   System.out.println("向服务器写\"开始考试\"失败");
  }
 }
 
 public void testTimerPerformed(){
  testTime -= 1000;
  timeLabel.setText(convertTime(testTime));
  if(testTime<=0){
   testTimer.stop();
   questionButton.setEnabled(false);
   answerButton.setEnabled(false);
  }
 }
  
 public void questionButtonPerformed(){
  questionButton.setEnabled(false);
  answerButton.setEnabled(true);
  try{
   outToServer.writeUTF("下一题");
  }
  catch(IOException ioe){
   System.out.println("向服务器写\"下一题\"失败");
  }
 }
 public void answerButtonPerformed(){
  String answer = "";
  questionButton.setEnabled(true);
  answerButton.setEnabled(false);
  for(int i = 0;i<radioButton.length;i++){
   if(radioButton[i].getState()){
    answer = radioButton[i].getLabel();
    break;
   }
  }
  try{
   outToServer.writeUTF("提交答案@"+answer);
  }
  catch(IOException ioe){
   System.out.println("向服务器\"提交答案\"失败");
  }
 }
 public void scoreButtonPerformed(){
  try{
   scoreButton.setEnabled(false);
   outToServer.writeUTF("成绩");
  }
  catch(IOException ioe){
   System.out.println("要求服务器发送\"成绩\"失败");
  }
 }
 
 private void socketClosing(){
  try{
   inFromServer.close();
   outToServer.close();
   connectToServer.close();
  }
  catch(Exception e){
   System.out.println("关闭socket异常!");
  }
 }
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

 }

}

ClientFrame.java

package ClientPart;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;

public class ClientFrame extends JFrame {
 TestPanel myPanel;
 
 public ClientFrame(String s){
  super(s);
  Container container = getContentPane();
  try{
   myPanel = new TestPanel(InetAddress.getLocalHost(),5500);
  }
  catch(UnknownHostException e){
   System.out.println("找不到主机:");
  }
  container.add(myPanel,BorderLayout.CENTER);
  setSize(400,200);
  setVisible(true);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

 
 public static void main(String[] args) {
  ClientFrame frame = new ClientFrame("C/S考试系统");
 }

}

阅读(7543) | 评论(3)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册