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

📄 exammainframe.java

📁 java实现的c/s模式的考试系统.比较简单
💻 JAVA
字号:
package exam.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.*;
import java.util.Timer;

import javax.swing.*;

import exam.dao.ScoreDao;
import exam.model.Paper;
import exam.model.Question;
import exam.model.Student;


/**
 * 考试主界面
 * @author teacher
 *
 */
public class ExamMainFrame extends JFrame implements ScoreDao{
	
	private Student student;
	private Paper paper;
	private QuestionPanel qPanel;
	private JLabel jlWelcome;
	private JLabel stuID;
	private JLabel stuName;
	private JLabel sbjName;
	private JLabel jlRNumDesc;
	private JLabel jlRNum;
	private int num;
	private JLabel jlRTimeDesc;
	private JLabel jlRTime;
	private int time;
	private Timer timer;
	private JLabel jlSlcDesc;
	private JRadioButton[] option;
	private JButton jbPre;
	private JButton jbNext;
	private JButton jbCarryOut;
	private int score;
	private ButtonGroup bg;
	
	private Socket s;
	private PrintWriter pw;
	
	public ExamMainFrame(Student student,Paper paper,Socket s ,PrintWriter pw){
		this.s = s;
		this.pw = pw;
		Font font = new Font("宋体",Font.BOLD,30);
		this.setTitle("欢迎进入达内考试系统--"+paper.getName());
		this.student = student;
		this.paper = paper;
		jlWelcome = new JLabel("欢迎进入达内考试系统");
		jlWelcome.setFont(font);
		stuID = new JLabel("    学生编号 : "+student.getId());
		stuName = new JLabel("    学生姓名 : "+student.getName());
		sbjName = new JLabel("     考试科目 : "+paper.getName());
		jlRNumDesc = new JLabel("剩余试题:    ");
		num = 0;
		jlRNum = new JLabel(""+(paper.getQuestions().length - num -1));
		jlRNum.setFont(font);
		
		qPanel = new QuestionPanel();
		qPanel.displayQuestion(num + 1, paper.getQuestions()[num]);
		
		time = 20*60;
		jlRTimeDesc = new JLabel("剩余时间:    ");
		jlRTime = new JLabel(""+time/60+":"+time%60);
		jlRTime.setFont(font);
		timer = new Timer();
		timer.scheduleAtFixedRate(new MyTimerTask(), 0, 1000);
		jlSlcDesc = new JLabel("请选择:");
		option = new JRadioButton[4];
		bg = new ButtonGroup();
		for(int i=0;i<4;i++){
			option[i] = new JRadioButton(""+(char)('A'+i));
			bg.add(option[i]);
		}
		jbPre = new JButton("<<上一题");
		jbPre.setEnabled(false);
		jbNext = new JButton(">>下一题");
		jbCarryOut = new JButton("交卷");
		score = 0;
		init();
		addEventHandle();
	}
	
	private void init(){
		JPanel jp1 = new JPanel();
		jp1.add(jlWelcome);
		jp1.add(stuID);
		jp1.add(stuName);
		jp1.add(sbjName);
		
		JPanel jp2 = new JPanel(new BorderLayout());
		JPanel jp2a = new JPanel();
		jp2a.add(jlRNumDesc);
		JPanel jp2b = new JPanel();
		jp2b.add(jlRNum);
		jp2.add(jp2a,BorderLayout.NORTH);
		jp2.add(jp2b,BorderLayout.CENTER);

		
		JPanel jp3 = new JPanel();
		jp3.add(qPanel);
		
		JPanel jp4 = new JPanel(new BorderLayout());
		JPanel jp4a = new JPanel();
		jp4a.add(jlRTimeDesc);
		JPanel jp4b = new JPanel();
		jp4b.add(jlRTime);
		jp4.add(jp4a,BorderLayout.NORTH);
		jp4.add(jp4b,BorderLayout.CENTER);
		
		JPanel jp5 = new JPanel();
		jp5.add(jlSlcDesc);
		for(int i=0;i<4;i++){
			jp5.add(option[i]);
		}
		jp5.add(jbPre);
		jp5.add(jbNext);
		jp5.add(jbCarryOut);
		
		this.setLayout(new BorderLayout());
		this.add(jp1,BorderLayout.NORTH);
		this.add(jp2,BorderLayout.WEST);
		this.add(jp3,BorderLayout.CENTER);
		this.add(jp4,BorderLayout.EAST);
		this.add(jp5,BorderLayout.SOUTH);
	}
	
	private void addEventHandle(){
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jbNext.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent e) {
				jbPre.setEnabled(true);
				addAnswer(num);
				num++;
				qPanel.displayQuestion(num+1, paper.getQuestions()[num]);
				jlRNum.setText(""+(paper.getQuestions().length - num - 1));
				if(num+1 == paper.getQuestions().length){
					jbNext.setEnabled(false);
				}
				bg.clearSelection();
			}
		});
		
		jbPre.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent e) {
				jbNext.setEnabled(true);
				addAnswer(num);
				num--;
				qPanel.displayQuestion(num + 1, paper.getQuestions()[num]);
				jlRNum.setText(""+(paper.getQuestions().length - num -1));
				if(num == 0){
					jbPre.setEnabled(false);
				}
				bg.clearSelection();
			}
		});
		
		jbCarryOut.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent e) {
				if(num < paper.getQuestions().length - 1){
					int op = JOptionPane.showConfirmDialog(ExamMainFrame.this,"您还有"+(paper.getQuestions().length - num -1)+"题未完成,确认提交吗?", "确认交卷",JOptionPane.YES_NO_OPTION);
					if(op == JOptionPane.YES_OPTION){
						addAnswer(num);
						num++;
						checkAnswer();
						
						addScore(student.getId(), score, paper.getName());
						System.exit(0);
					}
				}else{
					int op = JOptionPane.showConfirmDialog(ExamMainFrame.this,"您确认要提交试卷吗?", "确认交卷",JOptionPane.YES_NO_OPTION);
					if(op == JOptionPane.YES_OPTION){
						jbCarryOut.setEnabled(false);
						addAnswer(num);
						num++;
						checkAnswer();	
						String str = "%ADD_SCORE%:"+student.getId()+":"+score+":"+paper.getName();
//						addScore(student.getId(), score, paper.getName());
						try {
							pw.println(str);
							pw.flush();
							System.out.println("added score" + str);
							pw.close();
							s.close();
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						System.exit(0);
					}
				}
			}
		});
	}
	
	private void addAnswer(int num){
		for(int i=0;i<option.length;i++){
			if(option[i].isSelected()){
				paper.setAnswerSheet(num,(char)(i+'A'));
//				System.out.println((num+1)+"  "+(char)(i+'A'));
			}
		}	
	}
	
	private void checkAnswer(){
		for(int i=0;i<paper.getQuestions().length;i++){
			if(paper.getQuestions()[i].getTrueOption() == paper.getAnswerSheet()[i]){
				score++;
			}
		}
	}
	
	public void showMe(){
		this.setSize(800,600);
		this.setLocation(this.getToolkit().getScreenSize().width/2-this.getWidth()/2,this.getToolkit().getScreenSize().height/2-this.getHeight()/2); 
		this.setResizable(false);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		Student student = new Student(1001,"zhangsan");
		String [] option1 = {"private","long","sizeof","double"};
		String [] option2 = {"$abc","abc_def","3def","_abc"};
		Question[] questions = new Question[2];
		questions[0] = new Question("下列哪个单词不是Java的关键字?",option1,'C');
		questions[1] = new Question("下列哪个单词不能作为Java的标示符?",option2,'C');
		Paper paper = new Paper("corejava",questions);
		try {
			Socket s = new Socket("127.0.0.1",8888);
			new ExamMainFrame(student,paper,s,new PrintWriter(s.getOutputStream())).showMe();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void addScore(int id, int score, String subject) {
		String str = "%ADD_SCORE%:"+id+":"+score+":"+subject;
		pw.println(str);
		pw.flush();
		System.out.println(str);
	}
	
	class MyTimerTask extends TimerTask{
		@Override
		public void run() {
			time--;
			jlRTime.setText((time/60)+":"+(time%60));
			
			if(time == 0){
				timer.cancel();
				int op = JOptionPane.showConfirmDialog(ExamMainFrame.this, "考试时间结束!点击确定交卷.","时间结束",JOptionPane.YES_OPTION);
				if(op == JOptionPane.YES_OPTION){
					jbCarryOut.setEnabled(false);
					addAnswer(num);
					num++;
					checkAnswer();	
					String str = "%ADD_SCORE%:"+student.getId()+":"+score+":"+paper.getName();
//					addScore(student.getId(), score, paper.getName());
					try {
						pw.println(str);
						pw.flush();
						pw.close();
						s.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					System.exit(0);
				}
			}
		}
	}
}

⌨️ 快捷键说明

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