📄 selectquestion.java
字号:
package com.exam.ui.teacher;
import java.awt.Color;
import java.awt.Component;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
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.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
import com.exam.db.bean.Course;
import com.exam.db.bean.Question;
import com.exam.db.dao.CourseDao;
import com.exam.db.dao.QuestionDao;
import com.exam.ui.SuperFrame;
public class SelectQuestion extends SuperFrame {
private static final long serialVersionUID = 1L;
JTable tblQuestion;
int num = 0;
private JLabel lblPage;
private int currentPage = 1;
private int totalPage = 1;
private JButton btnFirstPage;
private JButton btnPreviousPage;
private JButton btnNextPage;
private JButton btnLastPage;
private List<Question> list;
public SelectQuestion() {
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 lblQuestionCouID = new JLabel("选择要查询的科目考题:");
lblQuestionCouID.setBounds(130, 20, 150, 20);
pnlTotal.add(lblQuestionCouID);
final JComboBox cboQuestionCouID = new JComboBox();
cboQuestionCouID.addItem("");
cboQuestionCouID.setBounds(280, 20, 180, 20);
pnlTotal.add(cboQuestionCouID);
List<Course> listCourse = new ArrayList<Course>();
CourseDao courseDao = new CourseDao();
listCourse = courseDao.selectAllCourse();
for (int i = 0; i < listCourse.size(); i++) {
cboQuestionCouID.addItem(listCourse.get(i).getGrade() + "-"
+ listCourse.get(i).getCouName());
}
JPanel pnlTop = new JPanel();
pnlTop.setBounds(20, 50, 550, 340);
pnlTop.setBorder(BorderFactory.createTitledBorder("考题信息"));
pnlTotal.add(pnlTop);
pnlTop.setLayout(null);
Object[][] data = new String[10][7];
Object[] columns = { "问题","题型", "答案A", "答案B", "答案C", "答案D", "正确答案" };
tblQuestion = new JTable(data, columns) {
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int row, int col) {
return false;
}
};
tblQuestion.setRowHeight(22);
JTableHeader tbhQuestion = tblQuestion.getTableHeader();
tbhQuestion.setBounds(10, 10, 500, 20);
tblQuestion.setBounds(10, 35, 500, 220);
tbhQuestion.setReorderingAllowed(false);
tblQuestion.setSelectionMode(0);
tblQuestion.setAutoResizeMode(0);
JScrollPane scpQuetion = new JScrollPane(tblQuestion);
scpQuetion.setBounds(25, 20, 500, 256);
scpQuetion
.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
pnlTop.add(scpQuetion);
TableColumnModel tcmQuestion = tblQuestion.getColumnModel();
tcmQuestion.getColumn(0).setMinWidth(200);
tcmQuestion.getColumn(1).setMinWidth(80);
tcmQuestion.getColumn(2).setMinWidth(120);
tcmQuestion.getColumn(3).setMinWidth(120);
tcmQuestion.getColumn(4).setMinWidth(120);
tcmQuestion.getColumn(5).setMinWidth(120);
tcmQuestion.getColumn(6).setMinWidth(120);
for (int i = 0; i < tblQuestion.getColumnCount(); i++) {
tcmQuestion.getColumn(i).setResizable(false);
}
DefaultTableCellRenderer dtcrSchedule = new DefaultTableCellRenderer() {
private static final long serialVersionUID = 1L;
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
if (row % 2 != 0) {
setBackground(new Color(206, 231, 255));
} else {
setBackground(new Color(255, 255, 255));
}
if(column==0){
setHorizontalAlignment(SwingConstants.LEFT);
}else{
setHorizontalAlignment(SwingConstants.CENTER);
}
return super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
}
};
for (int i = 0; i < tblQuestion.getColumnCount(); i++) {
tblQuestion.getColumn(columns[i]).setCellRenderer(dtcrSchedule);
}
lblPage = new JLabel("第 " + currentPage + " / " + totalPage + " 页");
btnFirstPage = new JButton("第一页");
btnPreviousPage = new JButton("上一页");
btnNextPage = new JButton("下一页");
btnLastPage = new JButton("最末页");
lblPage.setBounds(50, 300, 80, 25);
btnFirstPage.setBounds(140, 300, 60, 25);
btnFirstPage.setMargin(new Insets(0, 0, 0, 0));
btnPreviousPage.setBounds(230, 300, 60, 25);
btnPreviousPage.setMargin(new Insets(0, 0, 0, 0));
btnNextPage.setBounds(320, 300, 60, 25);
btnNextPage.setMargin(new Insets(0, 0, 0, 0));
btnLastPage.setBounds(410, 300, 60, 25);
btnLastPage.setMargin(new Insets(0, 0, 0, 0));
btnFirstPage.setEnabled(false);
btnPreviousPage.setEnabled(false);
btnNextPage.setEnabled(false);
btnLastPage.setEnabled(false);
pnlTop.add(lblPage);
pnlTop.add(btnFirstPage);
pnlTop.add(btnPreviousPage);
pnlTop.add(btnNextPage);
pnlTop.add(btnLastPage);
cboQuestionCouID.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0) {
currentPage = 1;
totalPage = 1;
if (cboQuestionCouID.getSelectedIndex() == 0) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 6; j++) {
tblQuestion.setValueAt("", i, j);
}
}
lblPage.setText("第 " + currentPage + " / " + totalPage
+ " 页");
if (currentPage == 1) {
btnFirstPage.setEnabled(false);
btnPreviousPage.setEnabled(false);
} else {
btnFirstPage.setEnabled(true);
btnPreviousPage.setEnabled(true);
}
if (totalPage == currentPage) {
btnNextPage.setEnabled(false);
btnLastPage.setEnabled(false);
} else {
btnNextPage.setEnabled(true);
btnLastPage.setEnabled(true);
}
} else {
String str = cboQuestionCouID.getSelectedItem().toString();
String couName = str.substring(3);
String grade = str.substring(0, 2);
CourseDao courseDao = new CourseDao();
Course course = new Course();
course = courseDao.selectCourseByNameAndGrade(couName,
grade);
QuestionDao quetionDao = new QuestionDao();
list = new ArrayList<Question>();
list = quetionDao.selectQuestionByCouID(course.getCouID());
showTblQuestion();
}
}
});
btnFirstPage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
currentPage = 1;
showTblQuestion();
}
});
btnPreviousPage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
currentPage--;
if (currentPage < 1) {
currentPage = 1;
}
showTblQuestion();
}
});
btnNextPage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
currentPage++;
if (currentPage == totalPage) {
currentPage = totalPage;
}
showTblQuestion();
}
});
btnLastPage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
currentPage = totalPage;
showTblQuestion();
}
});
}
private void showTblQuestion() {
if (list.size() % 10 == 0) {
if (list.size() == 0) {
totalPage = 1;
} else {
totalPage = list.size() / 10;
}
} else {
totalPage = list.size() / 10 + 1;
}
lblPage.setText("第 " + currentPage + " / " + totalPage + " 页");
if (currentPage == 1) {
btnFirstPage.setEnabled(false);
btnPreviousPage.setEnabled(false);
} else {
btnFirstPage.setEnabled(true);
btnPreviousPage.setEnabled(true);
}
if (totalPage == currentPage) {
btnNextPage.setEnabled(false);
btnLastPage.setEnabled(false);
} else {
btnNextPage.setEnabled(true);
btnLastPage.setEnabled(true);
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 4; j++) {
tblQuestion.setValueAt("", i, j);
}
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 6; j++) {
tblQuestion.setValueAt("", i, j);
}
}
Question question = new Question();
int first = (currentPage - 1) * 10;
for (int i = first; i < list.size(); i++) {
question = list.get(i);
String[] answer = question.getAnswer().split("@@");
tblQuestion.setValueAt(question.getQuestion(), i % 10, 0);
if (question.getQueType().equals("选择题")) {
tblQuestion.setValueAt("选择题", i % 10, 1);
tblQuestion.setValueAt(answer[0], i % 10, 2);
tblQuestion.setValueAt(answer[1], i % 10, 3);
tblQuestion.setValueAt(answer[2], i % 10, 4);
tblQuestion.setValueAt(answer[3], i % 10, 5);
} else if (question.getQueType().equals("判断题")) {
tblQuestion.setValueAt("判断题", i % 10, 1);
tblQuestion.setValueAt(answer[0], i % 10, 2);
tblQuestion.setValueAt(answer[1], i % 10, 3);
tblQuestion.setValueAt("", i % 10, 4);
tblQuestion.setValueAt("", i % 10, 5);
} else if (question.getQueType().equals("填空题")) {
tblQuestion.setValueAt("填空题", i % 10, 1);
tblQuestion.setValueAt(answer[0], i % 10, 2);
tblQuestion.setValueAt("", i % 10, 3);
tblQuestion.setValueAt("", i % 10, 4);
tblQuestion.setValueAt("", i % 10, 5);
}
tblQuestion.setValueAt(question.getRightAnswer(), i % 10, 6);
if ((i % 10) == 9) {
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -