📄 showallstudent.java
字号:
package com.exam.ui.student;
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.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
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.Student;
import com.exam.db.dao.StudentDao;
import com.exam.ui.SuperFrame;
public class ShowAllStudent extends SuperFrame {
private static final long serialVersionUID = 1L;
private int currentPage = 1;
private int totalPage = 1;
private JLabel lblPage;
private JButton btnFirstPage;
private JButton btnPreviousPage;
private JButton btnNextPage;
private JButton btnLastPage;
private List<Student> list;
private JTable tblStudent;
private String stuID;
public ShowAllStudent() {
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);
final JPopupMenu pop = new JPopupMenu();
JMenuItem mnuMore = new JMenuItem("成绩详细信息");
pop.add(mnuMore);
list = new ArrayList<Student>();
StudentDao studentDao = new StudentDao();
list = studentDao.selectAllStudent();
if (list.size() % 15 == 0) {
totalPage = list.size() / 15;
if (totalPage == 0) {
totalPage = 1;
}
} else {
totalPage = list.size() / 15 + 1;
}
JPanel pnlInfo = new JPanel();
pnlInfo.setBounds(20, 15, 550, 380);
pnlInfo.setBorder(BorderFactory.createTitledBorder("学生信息--共计"
+ list.size() + "人"));
pnlTotal.add(pnlInfo);
pnlInfo.setLayout(null);
Object[][] stuCells = new String[15][10];
Object[] stuCol = { "学号", "姓名", "性别", "年龄", "身份证号", "民族", "联系电话",
"家庭地址", "所属班级", "备注" };
tblStudent = new JTable(stuCells, stuCol){
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int row, int col) {
return false;
}
};
JTableHeader tbhStudent = tblStudent.getTableHeader();
tblStudent.setRowHeight(17);
tblStudent.setAutoResizeMode(0);
tbhStudent.setReorderingAllowed(false);
tblStudent.setSelectionMode(0);
TableColumnModel tcmStudent = tblStudent.getColumnModel();
tcmStudent.getColumn(2).setMinWidth(50);
tcmStudent.getColumn(2).setMaxWidth(50);
tcmStudent.getColumn(3).setMinWidth(50);
tcmStudent.getColumn(3).setMaxWidth(50);
tcmStudent.getColumn(4).setMinWidth(150);
tcmStudent.getColumn(6).setMinWidth(100);
tcmStudent.getColumn(7).setMinWidth(200);
tcmStudent.getColumn(8).setMinWidth(70);
tcmStudent.getColumn(9).setMinWidth(200);
for (int i = 0; i < tblStudent.getColumnCount(); i++) {
tcmStudent.getColumn(i).setResizable(false);
}
JScrollPane scpInfo = new JScrollPane(tblStudent);
scpInfo.setBounds(20, 30, 510, 291);
pnlInfo.add(scpInfo);
DefaultTableCellRenderer dtcrStudent = 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 == 7 || column == 9) {
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(stuCol[i]).setCellRenderer(dtcrStudent);
}
lblPage = new JLabel("第 " + currentPage + " / " + totalPage + " 页");
btnFirstPage = new JButton("第一页");
btnPreviousPage = new JButton("上一页");
btnNextPage = new JButton("下一页");
btnLastPage = new JButton("最末页");
JLabel lblEach = new JLabel("15条/页");
lblPage.setBounds(40, 335, 80, 25);
btnFirstPage.setBounds(130, 335, 60, 25);
btnFirstPage.setMargin(new Insets(0, 0, 0, 0));
btnPreviousPage.setBounds(210, 335, 60, 25);
btnPreviousPage.setMargin(new Insets(0, 0, 0, 0));
btnNextPage.setBounds(290, 335, 60, 25);
btnNextPage.setMargin(new Insets(0, 0, 0, 0));
btnLastPage.setBounds(370, 335, 60, 25);
btnLastPage.setMargin(new Insets(0, 0, 0, 0));
lblEach.setBounds(450, 335, 80, 25);
pnlInfo.add(lblPage);
pnlInfo.add(btnFirstPage);
pnlInfo.add(btnPreviousPage);
pnlInfo.add(btnNextPage);
pnlInfo.add(btnLastPage);
pnlInfo.add(lblEach);
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();
}
});
tblStudent.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (e.getButton() == MouseEvent.BUTTON3) {
pop.show(tblStudent, x, y);
}
if (e.getButton() == MouseEvent.BUTTON1) {
if (tblStudent.getValueAt(tblStudent.getSelectedRow(), 0) == null) {
} else {
stuID = tblStudent.getValueAt(
tblStudent.getSelectedRow(), 0).toString();
}
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
mnuMore.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (stuID == null) {
JOptionPane.showMessageDialog(null, "请选择一行数据!");
} else {
new ShowStudentScore(stuID);
}
}
});
}
private void showTblStudent() {
if (list.size() % 15 == 0) {
totalPage = list.size() / 15;
if (totalPage == 0) {
totalPage = 1;
}
} else {
totalPage = list.size() / 15 + 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 < 15; i++) {
for (int j = 0; j < 10; j++) {
tblStudent.setValueAt("", i, j);
}
}
Student student = new Student();
int first = (currentPage - 1) * 15;
for (int i = first; i < list.size(); i++) {
student = list.get(i);
tblStudent.setValueAt(student.getStuID(), i % 15, 0);
tblStudent.setValueAt(student.getStuName(), i % 15, 1);
tblStudent.setValueAt(student.getStuSex(), i % 15, 2);
tblStudent.setValueAt(student.getStuAge() + "", i % 15, 3);
tblStudent.setValueAt(student.getStuCardID(), i % 15, 4);
tblStudent.setValueAt(student.getStuNation(), i % 15, 5);
tblStudent.setValueAt(student.getStuPhone(), i % 15, 6);
tblStudent.setValueAt(student.getStuAddr(), i % 15, 7);
tblStudent.setValueAt(student.getBanJiID(), i % 15, 8);
tblStudent.setValueAt(student.getRemark(), i % 15, 9);
if ((i % 15) == 14) {
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -