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

📄 exam.java

📁 accp s1毕业项目 考试管理系统
💻 JAVA
字号:
package com.exam.ui.student;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.Timer;

import com.exam.db.bean.ExamDB;
import com.exam.db.bean.Question;
import com.exam.db.bean.Student;
import com.exam.db.dao.ExamDBDao;
import com.exam.db.dao.QuestionDao;
import com.exam.db.dao.StudentDao;
import com.exam.ui.SuperFrame;

public class Exam extends SuperFrame {
	private static final long serialVersionUID = 1L;
	private JTextField txtTime;
	private int minute = 19;
	private int second = 60;
	private JTextField txtNum;
	private Timer timer = null;
	private int current = 0;
	private int total = 19;
	private JButton btnPrevious;
	private JButton btnNext;
	private String stuID;
	private String couName;
	private int couID;
	private JTextField txtName;
	private JTextField txtSex;
	private JTextField txtGrade;
	private JTextField txtNo;
	private JTextArea txaQuestion;
	private JTextField txtAnswerA;
	private JTextField txtAnswerB;
	private JTextField txtAnswerC;
	private JTextField txtAnswerD;
	private JRadioButton radAnswerA;
	private JRadioButton radAnswerB;
	private JRadioButton radAnswerC;
	private JRadioButton radAnswerD;
	private JRadioButton radAnswerE;
	private ButtonQuestion[] btnAnswer;
	List<Question> list;

	public Exam(String stuID, int couID, String couName) {
		try {
			list = new ArrayList<Question>();
			QuestionDao questionDao = new QuestionDao();
			list = questionDao.selectQuestion(couID);
			if (list.size() < 20) {
				JOptionPane.showMessageDialog(null, "该科目还没有足够的考试试题!\n请联系监考教师!");
				dispose();
				return;
			}
			this.stuID = stuID;
			this.couID = couID;
			this.couName = couName;
			init();
			this.setVisible(true);
			this.setResizable(false);
			this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void init() {
		timer = new Timer(1000, new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				second--;
				if (second < 0) {
					minute--;
					second = 59;
				}
				if (second < 10 && minute < 10) {
					txtTime.setText("0" + minute + ":0" + second);
				} else if (second < 10) {
					txtTime.setText(minute + ":0" + second);
				} else if (minute < 10) {
					txtTime.setText("0" + minute + ":" + second);
				} else {
					txtTime.setText(minute + ":" + second);
				}
				if (minute == 0 && second == 0) {
					saveScore();
				}
			}
		});
		timer.start();

		this.setTitle("考试系统--学生考试");
		this.setSize(800, 600);
		this.setCenter();

		JPanel pnlTotal = new JPanel();
		this.getContentPane().add(pnlTotal);
		pnlTotal.setLayout(null);

		JPanel pnlTop = new JPanel();
		pnlTop.setBorder(BorderFactory.createTitledBorder("考生信息"));
		pnlTop.setBounds(35, 20, 720, 100);
		pnlTotal.add(pnlTop);
		pnlTop.setLayout(null);

		JLabel lblName = new JLabel("姓名");
		lblName.setBounds(75, 25, 40, 25);
		pnlTop.add(lblName);

		txtName = new JTextField();
		txtName.setBounds(115, 25, 80, 25);
		txtName.setEditable(false);
		pnlTop.add(txtName);

		JLabel lblNo = new JLabel("学号");
		lblNo.setBounds(280, 25, 40, 25);
		pnlTop.add(lblNo);

		txtNo = new JTextField();
		txtNo.setBounds(320, 25, 80, 25);
		txtNo.setEditable(false);
		pnlTop.add(txtNo);

		JLabel lblSex = new JLabel("性别");
		lblSex.setBounds(75, 60, 40, 25);
		pnlTop.add(lblSex);

		txtSex = new JTextField();
		txtSex.setBounds(115, 60, 80, 25);
		txtSex.setEditable(false);
		pnlTop.add(txtSex);

		JLabel lblGrade = new JLabel("班级");
		lblGrade.setBounds(280, 60, 40, 25);
		pnlTop.add(lblGrade);

		txtGrade = new JTextField();
		txtGrade.setBounds(320, 60, 80, 25);
		txtGrade.setEditable(false);
		pnlTop.add(txtGrade);

		JLabel lblTime = new JLabel("剩余时间:");
		lblTime.setBounds(480, 42, 60, 25);
		pnlTop.add(lblTime);

		txtTime = new JTextField();
		txtTime.setBounds(540, 42, 40, 25);
		txtTime.setHorizontalAlignment(JTextField.CENTER);
		txtTime.setForeground(Color.red);
		txtTime.setEditable(false);
		pnlTop.add(txtTime);

		JPanel pnlMain = new JPanel();
		pnlMain.setBorder(BorderFactory.createTitledBorder("试题信息--科目:"
				+ couName));
		pnlMain.setBounds(35, 130, 720, 300);
		pnlTotal.add(pnlMain);
		pnlMain.setLayout(null);

		txaQuestion = new JTextArea();
		txaQuestion.setLineWrap(true);
		txaQuestion.setEditable(false);
		JScrollPane scpQuestion = new JScrollPane(txaQuestion);
		scpQuestion.setBounds(40, 20, 650, 100);
		pnlMain.add(scpQuestion);

		radAnswerA = new JRadioButton("A");
		radAnswerA.setBounds(35, 130, 50, 25);
		pnlMain.add(radAnswerA);

		txtAnswerA = new JTextField();
		txtAnswerA.setBounds(90, 130, 600, 25);
		txtAnswerA.setEditable(false);
		pnlMain.add(txtAnswerA);

		radAnswerB = new JRadioButton("B");
		radAnswerB.setBounds(35, 160, 50, 25);
		pnlMain.add(radAnswerB);

		txtAnswerB = new JTextField();
		txtAnswerB.setBounds(90, 160, 600, 25);
		txtAnswerB.setEditable(false);
		pnlMain.add(txtAnswerB);

		radAnswerC = new JRadioButton("C");
		radAnswerC.setBounds(35, 190, 50, 25);
		pnlMain.add(radAnswerC);

		txtAnswerC = new JTextField();
		txtAnswerC.setBounds(90, 190, 600, 25);
		txtAnswerC.setEditable(false);
		pnlMain.add(txtAnswerC);

		radAnswerD = new JRadioButton("D");
		radAnswerD.setBounds(35, 220, 50, 25);
		pnlMain.add(radAnswerD);

		txtAnswerD = new JTextField();
		txtAnswerD.setBounds(90, 220, 600, 25);
		txtAnswerD.setEditable(false);
		pnlMain.add(txtAnswerD);

		radAnswerE = new JRadioButton("E");
		radAnswerE.setBounds(35, 250, 50, 25);
		radAnswerE.setVisible(false);
		pnlMain.add(radAnswerE);

		ButtonGroup btgAnswer = new ButtonGroup();
		btgAnswer.add(radAnswerA);
		btgAnswer.add(radAnswerB);
		btgAnswer.add(radAnswerC);
		btgAnswer.add(radAnswerD);
		btgAnswer.add(radAnswerE);

		btnPrevious = new JButton("上一题");
		btnNext = new JButton("下一题");
		JButton btnHand = new JButton("交卷");
		btnPrevious.setMargin(new Insets(0, 0, 0, 0));
		btnNext.setMargin(new Insets(0, 0, 0, 0));
		btnHand.setMargin(new Insets(0, 0, 0, 0));
		btnPrevious.setBounds(180, 260, 60, 25);
		btnNext.setBounds(280, 260, 60, 25);
		btnHand.setBounds(560, 260, 60, 25);
		pnlMain.add(btnPrevious);
		pnlMain.add(btnNext);
		pnlMain.add(btnHand);

		JPanel pnlBottom = new JPanel();
		pnlBottom.setBorder(BorderFactory.createTitledBorder("答题信息"));
		pnlBottom.setBounds(35, 440, 720, 100);
		pnlTotal.add(pnlBottom);
		pnlBottom.setLayout(null);

		JPanel pnlBottomLeft = new JPanel();
		pnlBottomLeft.setBounds(40, 30, 400, 50);
		pnlBottom.add(pnlBottomLeft);
		pnlBottomLeft.setLayout(new GridLayout(2, 10));

		btnAnswer = new ButtonQuestion[20];
		for (int i = 0; i < 20; i++) {
			btnAnswer[i] = new ButtonQuestion(i);
			btnAnswer[i].setText(i + 1 + "");
			btnAnswer[i].setMargin(new Insets(0, 0, 0, 0));
			pnlBottomLeft.add(btnAnswer[i]);
			final int num = i;
			btnAnswer[i].addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					checkQuestionType();
					checkIsDo();					
					current = num;
					showQuestion(num);
				}
			});
		}

		JLabel lblPrompt1 = new JLabel("你还有");
		lblPrompt1.setBounds(560, 40, 40, 25);
		pnlBottom.add(lblPrompt1);

		txtNum = new JTextField();
		txtNum.setBounds(600, 40, 20, 25);
		txtNum.setEditable(false);
		txtNum.setText("20");
		txtNum.setForeground(Color.red);
		pnlBottom.add(txtNum);

		JLabel lblPrompt2 = new JLabel("题未完成!");
		lblPrompt2.setBounds(620, 40, 80, 25);
		pnlBottom.add(lblPrompt2);

		showStuInfo();
		showQuestion(current);

		btnPrevious.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				checkQuestionType();
				checkIsDo();				
				current--;
				showQuestion(current);
			}
		});
		btnNext.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				checkQuestionType();
				checkIsDo();
				current++;
				showQuestion(current);
			}
		});
		btnHand.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int result = JOptionPane.showConfirmDialog(null, "确认交卷!", "确认",
						JOptionPane.YES_NO_OPTION);
				if (result == JOptionPane.YES_OPTION) {
					saveScore();
				} else {
					return;
				}
			}
		});
	}
	
	private void checkQuestionType(){		
		if(list.get(current).getQueType().equals("填空题")){
			if(txtAnswerA.getText().equals("")){
				JOptionPane.showMessageDialog(null, "你还没有写答案哦!\n题号:"+(current+1));				
				return;
			}
		}
	}

	private void saveScore() {
		checkQuestionType();
		checkIsDo();
		int score = 0;
		StringBuffer question = new StringBuffer();
		StringBuffer answer = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			String rightAnswer = list.get(i).getRightAnswer().toString();
			if (rightAnswer.equals(btnAnswer[i].getAnswer())) {
				score += list.get(i).getMark();
			}
			question.append(list.get(i).getQueID());
			if (btnAnswer[i].getAnswer() == null) {
				btnAnswer[i].setAnswer("");
			}
			answer.append(btnAnswer[i].getAnswer());
			if (i != list.size() - 1) {
				question.append("@@");
				answer.append("@@");
			}
		}
		ExamDBDao examDBDao = new ExamDBDao();
		ExamDB examDB = new ExamDB();
		examDB.setCouID(couID);
		examDB.setStuID(stuID);
		examDB.setQuestion(question.toString());
		examDB.setAnswer(answer.toString());
		examDB.setScore(score);
		int result = examDBDao.insertExamDB(examDB);
		timer.stop();
		if (result == 1) {
			JOptionPane.showMessageDialog(null, "你此次考试得分:" + score);
			dispose();
		} else {
			saveScore();
		}
	}

	private void showStuInfo() {
		StudentDao studentDao = new StudentDao();
		Student student = new Student();
		student = studentDao.selectStudentByStuID(stuID);
		txtName.setText(student.getStuName());
		txtNo.setText(stuID);
		txtSex.setText(student.getStuSex());
		txtGrade.setText(student.getBanJiID());
	}

	private void showQuestion(int num) {
		showCurrentQuestion();
		txaQuestion.setText("");
		txtAnswerA.setText("");
		txtAnswerB.setText("");
		txtAnswerC.setText("");
		txtAnswerD.setText("");
		if (current == 0) {
			btnPrevious.setEnabled(false);
		} else {
			btnPrevious.setEnabled(true);
		}
		if (total == current) {
			btnNext.setEnabled(false);
		} else {
			btnNext.setEnabled(true);
		}
		Question question = list.get(num);
		String[] answer = question.getAnswer().split("@@");

		if (question.getQueType().equals("选择题")) {
			txaQuestion.setText(question.getQuestion());
			txtAnswerA.setText(answer[0]);
			txtAnswerB.setText(answer[1]);
			txtAnswerC.setText(answer[2]);
			txtAnswerD.setText(answer[3]);
			txtAnswerA.setEditable(false);

			radAnswerA.setEnabled(true);
			radAnswerB.setEnabled(true);
			radAnswerC.setEnabled(true);
			radAnswerD.setEnabled(true);

		} else if (question.getQueType().equals("判断题")) {
			txaQuestion.setText(question.getQuestion());
			txtAnswerA.setText(answer[0]);
			txtAnswerB.setText(answer[1]);
			txtAnswerA.setEditable(false);

			radAnswerA.setEnabled(true);
			radAnswerB.setEnabled(true);
			radAnswerC.setEnabled(false);
			radAnswerD.setEnabled(false);

		} else if (question.getQueType().equals("填空题")) {
			txaQuestion.setText(question.getQuestion());
			txtAnswerA.setEditable(true);
			txtAnswerA.setText(btnAnswer[current].getAnswer());

			radAnswerA.setEnabled(false);
			radAnswerB.setEnabled(false);
			radAnswerC.setEnabled(false);
			radAnswerD.setEnabled(false);
		}
		if (btnAnswer[current].isDo) {
			if (btnAnswer[current].getAnswer().equals(txtAnswerA.getText())) {
				radAnswerA.setSelected(true);
			} else if (btnAnswer[current].getAnswer().equals(
					txtAnswerB.getText())) {
				radAnswerB.setSelected(true);
			} else if (btnAnswer[current].getAnswer().equals(
					txtAnswerC.getText())) {
				radAnswerC.setSelected(true);
			} else if (btnAnswer[current].getAnswer().equals(
					txtAnswerD.getText())) {
				radAnswerD.setSelected(true);
			}
			System.out.println(btnAnswer[current]);
		} else {
			radAnswerE.setSelected(true);
		}
		if (question.getQueType().equals("填空题")) {
			radAnswerA.setSelected(true);
		}
	}

	private void checkIsDo() {
		if (radAnswerA.isSelected()) {
			btnAnswer[current].setBackground(Color.green);
			btnAnswer[current].isDo = true;
			btnAnswer[current].setAnswer(txtAnswerA.getText());
		} else if (radAnswerB.isSelected()) {
			btnAnswer[current].setBackground(Color.green);
			btnAnswer[current].isDo = true;
			btnAnswer[current].setAnswer(txtAnswerB.getText());
		} else if (radAnswerC.isSelected()) {
			btnAnswer[current].setBackground(Color.green);
			btnAnswer[current].isDo = true;
			btnAnswer[current].setAnswer(txtAnswerC.getText());
		} else if (radAnswerD.isSelected()) {
			btnAnswer[current].setBackground(Color.green);
			btnAnswer[current].isDo = true;
			btnAnswer[current].setAnswer(txtAnswerD.getText());
		}
		int lastQuestion = 20;
		for (int i = 0; i < 20; i++) {
			if (btnAnswer[i].isDo) {
				lastQuestion--;
			}
			txtNum.setText(lastQuestion + "");
		}
	}

	private void showCurrentQuestion() {
		for (int i = 0; i < btnAnswer.length; i++) {
			if (i == current) {
				btnAnswer[i].setBackground(Color.red);
			} else {
				if (btnAnswer[i].isDo) {
				} else {
					btnAnswer[i].setBackground(null);
				}
			}
		}
	}
}

⌨️ 快捷键说明

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