📄 showstudentscore.java
字号:
package com.exam.ui.student;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
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.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
//import javax.swing.table.TableRowSorter;
import com.exam.db.bean.Course;
import com.exam.db.bean.ExamDB;
import com.exam.db.bean.Student;
import com.exam.db.dao.CourseDao;
import com.exam.db.dao.ExamDBDao;
import com.exam.db.dao.StudentDao;
import com.exam.ui.SuperFrame;
public class ShowStudentScore extends SuperFrame {
private int currentPage = 1;
private int totalPage = 1;
private JButton btnFirstPage;
private JButton btnPreviousPage;
private JButton btnNextPage;
private JButton btnLastPage;
private JTable tblStudent;
private List<ExamDB> list;
private JLabel lblPage;
private static final long serialVersionUID = 1L;
private String stuID;
public ShowStudentScore(String stuID) {
this.stuID = stuID;
ExamDBDao examDBDao = new ExamDBDao();
list = new ArrayList<ExamDB>();
list = examDBDao.selectExamDBByStuID(stuID);
init();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
@SuppressWarnings("unchecked")
public void init() {
this.setTitle("学生成绩信息");
this.setSize(600, 400);
this.setResizable(false);
this.setCenter();
Student student = new Student();
student = new StudentDao().selectStudentByStuID(stuID);
Container ct = this.getContentPane();
ct.setLayout(null);
JLabel lblStuName = new JLabel("姓 名");
lblStuName.setBounds(70, 30, 70, 20);
ct.add(lblStuName);
final JTextField txtStuName = new JTextField();
txtStuName.setBounds(140, 30, 130, 20);
txtStuName.setText(student.getStuName());
txtStuName.setEditable(false);
ct.add(txtStuName);
JLabel lblStuID = new JLabel("学 号");
lblStuID.setBounds(325, 30, 70, 20);
ct.add(lblStuID);
final JTextField txtStuID = new JTextField();
txtStuID.setBounds(390, 30, 130, 20);
txtStuID.setText(student.getStuID());
txtStuID.setEditable(false);
ct.add(txtStuID);
Object[][] data = new String[10][4];
Object[] columns = { "科 目", "成 绩", "最 高 分", "平 均 分" };
tblStudent = new JTable(data, columns){
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int arg0, int arg1) {
return false;
}
};
JTableHeader header = tblStudent.getTableHeader();
header.setReorderingAllowed(false);
tblStudent.setSelectionMode(0);
header.setBounds(70, 70, 450, 24);
tblStudent.setBounds(70, 94, 450, 200);
ct.add(header);
ct.add(tblStudent);
tblStudent.setRowHeight(20);
// tblStudent.setRowSorter(sorter);
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 < tblStudent.getColumnCount(); i++) {
tblStudent.getColumn(columns[i]).setCellRenderer(dtcrSchedule);
}
lblPage = new JLabel("第 " + currentPage + " / " + totalPage + " 页");
btnFirstPage = new JButton("第一页");
btnPreviousPage = new JButton("上一页");
btnNextPage = new JButton("下一页");
btnLastPage = new JButton("最末页");
JButton btnExit = new JButton("确定");
lblPage.setBounds(80, 320, 80, 25);
btnFirstPage.setBounds(150, 320, 60, 25);
btnFirstPage.setMargin(new Insets(0, 0, 0, 0));
btnPreviousPage.setBounds(225, 320, 60, 25);
btnPreviousPage.setMargin(new Insets(0, 0, 0, 0));
btnNextPage.setBounds(300, 320, 60, 25);
btnNextPage.setMargin(new Insets(0, 0, 0, 0));
btnLastPage.setBounds(375, 320, 60, 25);
btnLastPage.setMargin(new Insets(0, 0, 0, 0));
btnExit.setBounds(450, 320, 60, 25);
btnExit.setMargin(new Insets(0,0,0,0));
ct.add(lblPage);
ct.add(btnFirstPage);
ct.add(btnPreviousPage);
ct.add(btnNextPage);
ct.add(btnLastPage);
ct.add(btnExit);
TableColumnModel modelAll = tblStudent.getColumnModel();
modelAll.getColumn(0);
modelAll.getColumn(1).setMaxWidth(50);
modelAll.getColumn(2).setMaxWidth(50);
modelAll.getColumn(3).setMaxWidth(50);
for (int i = 0; i < tblStudent.getColumnCount(); i++) {
modelAll.getColumn(i).setResizable(false);
}
showTblStudent();
btnFirstPage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
currentPage = 1;
showTblStudent();
}
});
btnPreviousPage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
currentPage--;
if (currentPage < 1) {
currentPage = 1;
}
showTblStudent();
}
});
btnNextPage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
currentPage++;
if (currentPage == totalPage) {
currentPage = totalPage;
}
showTblStudent();
}
});
btnLastPage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
currentPage = totalPage;
showTblStudent();
}
});
btnExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
}
private void showTblStudent() {
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++) {
tblStudent.setValueAt("", i, j);
}
}
CourseDao courseDao = new CourseDao();
Course course = new Course();
ExamDB examDB = new ExamDB();
String str = stuID.substring(0, 7) + "%";
int first = (currentPage - 1) * 10;
for (int i = first; i < list.size(); i++) {
examDB = list.get(i);
int couID = examDB.getCouID();
course = courseDao.selectCourseNameByID(couID);
tblStudent.setValueAt(course.getCouName(), i, 0);
tblStudent.setValueAt(examDB.getScore() + "", i, 1);
ExamDBDao examDBDaoTotal = new ExamDBDao();
List<ExamDB> listTotal = examDBDaoTotal
.selectAvrScoreByStuIDAndCouID(str, couID);
int sum = 0;
int max = 0;
for (int j = 0; j < listTotal.size(); j++) {
int score = listTotal.get(j).getScore();
sum += score;
if (max < score) {
max = score;
}
}
tblStudent.setValueAt(max + "", i, 2);
tblStudent.setValueAt((float) (sum / listTotal.size()) + "", i, 3);
if ((i % 10) == 9) {
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -