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

📄 testpanel.java~25~

📁 这是我最得意的一件小作品。它是一个基于C/S模式的网络考试系统
💻 JAVA~25~
字号:

package testsystem;

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 addr,int port)
    {
        initPanelGUI();
       try {
           connectToServer=new Socket(addr,port);
           inFromServer=new DataInputStream(connectToServer.getInputStream());
           outToServer=new DataOutputStream(connectToServer.getOutputStream());
           System.out.println(connectToServer.getInetAddress());
       } catch (IOException ex) {
           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("考试时间"))
                {
                    System.out.println(inStr.indexOf('@')+1);
                    inStr=inStr.substring(inStr.indexOf('@')+1);
                    testTime=Integer.parseInt(inStr);
                    timeLabel.setText(convertTime(testTime));
                    System.out.println(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 ex) {
                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.setWrapStyleWord(true);
        questionArea.setFont(new Font("幼圆",Font.PLAIN,16));
        int vScroll=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
        int hScroll=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;
        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("下一题");
        questionButton.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;

    }

    private void startButtonPerformed()
    {
        startButton.setEnabled(true);
        questionButton.setEnabled(false);
        answerButton.setEnabled(true);
        try {
            outToServer.writeUTF("开始考试");
        } catch (Exception ex) {
            System.out.println("向服务器写\"开始考试\"失败");
        }
    }

    private void testTimerPerformed()
    {
        testTime-=1000;
        timeLabel.setText(convertTime(testTime));
        if(testTime<=0)
        {
            testTimer.stop();
            questionButton.setEnabled(false);
            answerButton.setEnabled(false);
            JOptionPane.showMessageDialog(null,"对不起,时间已到",null,JOptionPane.INFORMATION_MESSAGE);
            //JOptionPane.showMessageDialog(null,inStr,"成绩显示",JOptionPane.INFORMATION_MESSAGE);

        }
    }

    private void questionButtonPerformed()
    {
        questionButton.setEnabled(false);
        answerButton.setEnabled(true);
        try {
            outToServer.writeUTF("下一题");

        } catch (Exception ex) {
            System.out.println("向服务器写\"下一题\"失败");
        }
    }

    private 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 (Exception e) {
            System.out.println("向服务器\"提交答案\"失败");
        }
    }

    private void scoreButtonPerformed()
    {
        try {
            scoreButton.setEnabled(false);
            outToServer.writeUTF("成绩");
        } catch (Exception ex) {
            System.out.println("要求服务器发送\" 成绩\"失败");
        }
    }

    private void socketClosing()
    {
        try {
            inFromServer.close();
            outToServer.close();
            connectToServer.close();
        } catch (Exception ex) {
            System.out.println("关闭socket发生异常!");
        }
    }

}

⌨️ 快捷键说明

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