📄 selectpaper.java
字号:
package com.exam.ui.teacher;
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.JButton;
import javax.swing.JComboBox;
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 com.exam.db.bean.Course;
import com.exam.db.bean.ExamDB;
import com.exam.db.bean.Question;
import com.exam.db.bean.Student;
import com.exam.db.dao.CourseDao;
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 SelectPaper extends SuperFrame {
private static final long serialVersionUID = 1L;
private int current = 0;
private int total = 19;
private JTextArea txaQuestion;
private JTextField txtAnswerA;
private JTextField txtAnswerB;
private JTextField txtAnswerC;
private JTextField txtAnswerD;
private JTextField txtRightAnswer;
private JTextField txtStuAnswer;
private JButton btnPrevious;
private JButton btnNext;
private JButton[] btnQuestion;
private ExamDB examDB;
private String[] queID;
private String[] stuAnswer;
public SelectPaper() {
try {
init();
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
} catch (Exception e) {
e.printStackTrace();
}
}
private void init() {
this.setTitle("试卷");
this.setSize(600, 450);
this.setCenter();
JPanel pnlTotal = new JPanel();
this.getContentPane().add(pnlTotal);
pnlTotal.setLayout(null);
JLabel lblCouName = new JLabel("考试科目");
lblCouName.setBounds(50, 20, 60, 25);
pnlTotal.add(lblCouName);
final JComboBox cboCouName = new JComboBox();
cboCouName.setBounds(110, 20, 120, 25);
cboCouName.addItem("");
pnlTotal.add(cboCouName);
List<Course> listCourse = new ArrayList<Course>();
CourseDao courseDao = new CourseDao();
listCourse = courseDao.selectAllCourse();
for (int i = 0; i < listCourse.size(); i++) {
cboCouName.addItem(listCourse.get(i).getGrade() + "-"
+ listCourse.get(i).getCouName());
}
JLabel lblStuID = new JLabel("学生学号");
lblStuID.setBounds(250, 20, 60, 25);
pnlTotal.add(lblStuID);
final JComboBox cboStuID = new JComboBox();
cboStuID.setBounds(310, 20, 120, 25);
cboStuID.setEditable(true);
cboStuID.addItem("");
pnlTotal.add(cboStuID);
List<Student> listStudent = new ArrayList<Student>();
StudentDao studentDao = new StudentDao();
listStudent = studentDao.selectAllStudent();
for (Student student : listStudent) {
cboStuID.addItem(student.getStuID());
}
JButton btnFind = new JButton("查找");
btnFind.setBounds(480, 20, 60, 25);
btnFind.setMargin(new Insets(0, 0, 0, 0));
pnlTotal.add(btnFind);
JPanel pnlInfo = new JPanel();
pnlInfo.setBounds(30, 50, 530, 350);
pnlInfo.setBorder(BorderFactory.createTitledBorder("试卷"));
pnlTotal.add(pnlInfo);
pnlInfo.setLayout(null);
txaQuestion = new JTextArea();
txaQuestion.setLineWrap(true);
txaQuestion.setEditable(false);
txaQuestion.setForeground(Color.black);
JScrollPane scpQuestion = new JScrollPane(txaQuestion);
scpQuestion.setBounds(20, 30, 490, 60);
pnlInfo.add(scpQuestion);
JRadioButton radAnswerA = new JRadioButton("A");
radAnswerA.setBounds(30, 100, 40, 25);
radAnswerA.setEnabled(false);
pnlInfo.add(radAnswerA);
txtAnswerA = new JTextField();
txtAnswerA.setBounds(80, 100, 428, 25);
txtAnswerA.setEditable(false);
pnlInfo.add(txtAnswerA);
JRadioButton radAnswerB = new JRadioButton("B");
radAnswerB.setBounds(30, 130, 40, 25);
radAnswerB.setEnabled(false);
pnlInfo.add(radAnswerB);
txtAnswerB = new JTextField();
txtAnswerB.setBounds(80, 130, 428, 25);
txtAnswerB.setEditable(false);
pnlInfo.add(txtAnswerB);
JRadioButton radAnswerC = new JRadioButton("C");
radAnswerC.setBounds(30, 160, 40, 25);
radAnswerC.setEnabled(false);
pnlInfo.add(radAnswerC);
txtAnswerC = new JTextField();
txtAnswerC.setBounds(80, 160, 428, 25);
txtAnswerC.setEditable(false);
pnlInfo.add(txtAnswerC);
JRadioButton radAnswerD = new JRadioButton("D");
radAnswerD.setBounds(30, 190, 40, 25);
radAnswerD.setEnabled(false);
pnlInfo.add(radAnswerD);
txtAnswerD = new JTextField();
txtAnswerD.setBounds(80, 190, 428, 25);
txtAnswerD.setEditable(false);
pnlInfo.add(txtAnswerD);
JLabel lblStuAnswer = new JLabel("学生答案");
lblStuAnswer.setBounds(20, 220, 60, 25);
pnlInfo.add(lblStuAnswer);
txtStuAnswer = new JTextField();
txtStuAnswer.setBounds(80, 220, 300, 25);
txtStuAnswer.setEditable(false);
pnlInfo.add(txtStuAnswer);
JLabel lblRightAnswer = new JLabel("正确答案");
lblRightAnswer.setBounds(20, 250, 60, 25);
pnlInfo.add(lblRightAnswer);
txtRightAnswer = new JTextField();
txtRightAnswer.setBounds(80, 250, 300, 25);
txtRightAnswer.setEditable(false);
pnlInfo.add(txtRightAnswer);
btnPrevious = new JButton("上一题");
btnNext = new JButton("下一题");
btnPrevious.setMargin(new Insets(0, 0, 0, 0));
btnNext.setMargin(new Insets(0, 0, 0, 0));
btnPrevious.setBounds(420, 220, 60, 25);
btnNext.setBounds(420, 250, 60, 25);
btnPrevious.setEnabled(false);
btnNext.setEnabled(false);
pnlInfo.add(btnPrevious);
pnlInfo.add(btnNext);
JPanel pnlButton = new JPanel();
pnlButton.setBounds(70, 290, 400, 50);
pnlInfo.add(pnlButton);
pnlButton.setLayout(new GridLayout(2, 10, 1, 1));
btnQuestion = new JButton[20];
for (int i = 0; i < btnQuestion.length; i++) {
btnQuestion[i] = new JButton(i + 1 + "");
btnQuestion[i].setMargin(new Insets(0, 0, 0, 0));
btnQuestion[i].setEnabled(false);
pnlButton.add(btnQuestion[i]);
final int num = i;
btnQuestion[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
current = num;
showQuestion(num);
}
});
}
btnFind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (cboCouName.getSelectedIndex() == 0) {
JOptionPane.showMessageDialog(null, "请选择科目!");
return;
}
if (cboStuID.getSelectedItem().toString().equals("")) {
JOptionPane.showMessageDialog(null, "请选择学号!");
return;
}
CourseDao courseDao = new CourseDao();
String str = cboCouName.getSelectedItem().toString().trim();
String couName = str.substring(3);
String grade = str.substring(0, 2);
Course course = courseDao.selectCourseByNameAndGrade(couName,
grade);
int couID = course.getCouID();
String stuID = cboStuID.getSelectedItem().toString().trim();
boolean result = getPaper(stuID, couID);
if (result) {
} else {
return;
}
current = 0;
showQuestion(current);
}
});
btnPrevious.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
current--;
showQuestion(current);
}
});
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
current++;
showQuestion(current);
}
});
}
private boolean getPaper(String stuID, int couID) {
ExamDBDao examDBDao = new ExamDBDao();
List<ExamDB> list = examDBDao.selectExamDBByStuIDAndCouID(stuID, couID);
if (list.size() == 0) {
JOptionPane.showMessageDialog(null, "该学生没有进行此科目的考试");
return false;
} else {
JOptionPane.showMessageDialog(null, "该学生该科目考了" + list.size()
+ "次!\n将显示最后一次考试试卷!");
examDB = list.get(0);
for (int i = 0; i < btnQuestion.length; i++) {
btnQuestion[i].setEnabled(true);
}
btnPrevious.setEnabled(true);
btnNext.setEnabled(true);
queID = examDB.getQuestion().split("@@");
stuAnswer = examDB.getAnswer().split("@@");
}
return true;
}
private void showQuestion(int num) {
showCurrentQuestion();
QuestionDao questionDao = new QuestionDao();
Question question = new Question();
question = questionDao.selectQuestionByQueID(Integer
.parseInt(queID[current]));
txaQuestion.setText(question.getQuestion());
String[] radAnswer = question.getAnswer().split("@@");
if (question.getQueType().equals("选择题")) {
txtAnswerA.setText(radAnswer[0]);
txtAnswerB.setText(radAnswer[1]);
txtAnswerC.setText(radAnswer[2]);
txtAnswerD.setText(radAnswer[3]);
} else if (question.getQueType().equals("判断题")) {
txtAnswerA.setText(radAnswer[0]);
txtAnswerB.setText(radAnswer[1]);
txtAnswerC.setText("");
txtAnswerD.setText("");
} else if (question.getQueType().equals("填空题")) {
txtAnswerA.setText("");
txtAnswerB.setText("");
txtAnswerC.setText("");
txtAnswerD.setText("");
}
txtStuAnswer.setText(stuAnswer[current]);
txtRightAnswer.setText(question.getRightAnswer());
}
private void showCurrentQuestion() {
if (current == 0) {
btnPrevious.setEnabled(false);
} else {
btnPrevious.setEnabled(true);
}
if (current == total) {
btnNext.setEnabled(false);
} else {
btnNext.setEnabled(true);
}
for (int i = 0; i < btnQuestion.length; i++) {
if (i == current) {
btnQuestion[i].setBackground(Color.red);
} else {
btnQuestion[i].setBackground(null);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -