package studentSM.gui;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import org.apache.ibatis.session.SqlSession;
import studentSM.bean.Student;
import studentSM.dao.mapper;
import studentSM.tool.SqlSessionFactoryTool;
@SuppressWarnings("serial")
public class selete extends JPanel implements ActionListener {
private JTable table;
private JLabel jl_title, jl_name, jl_id, jl_di, jl_ye, jl_gong;
private JTextField jtf_id, jtf_name, jft_go;
private JPanel panel;
private JButton jb_first, jb_up, jb_down, jb_seach, jb_jump;
private JScrollPane line;
private DefaultTableModel stuTable;
private int page = 1, limit = 10, fristPage = 1, lastPage, count;
private List<Student> students;
private String[][] result = null;
private SqlSession session = null;
private static String[] columnNames = { "学号", "姓名", "性别", "生日", "年龄", "系别", "宿舍" };
private JButton jb_last;
public JPanel getSelectJPanel() {
try {
session = new SqlSessionFactoryTool().getSqlSession();
mapper mapper = session.getMapper(mapper.class);
students = mapper.selectStuBySomething(null, null, (page - 1) * limit, limit);
count = mapper.countStu(null, null);
lastPage = count / limit + 1;
} catch (IOException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "初始化失败!", JOptionPane.ERROR_MESSAGE);
} finally {
session.close();
}
setLayout(null);
panel = new JPanel();
panel.setSize(554, 481);
panel.setLayout(null);
jl_title = new JLabel("学生信息查询");
jl_title.setFont(new Font("微软雅黑", Font.PLAIN, 26));
jl_title.setBounds(174, 10, 165, 34);
panel.add(jl_title);
// 表格显示
result = toArray(students);
// 转为二维数组
stuTable = new DefaultTableModel(result, columnNames) {
public boolean isCellEditable(int row, int column) {
return false;
}
};// 可以被选中但不可被编辑
table = new JTable(stuTable);
setWidth();
table.setEnabled(false);
line = new JScrollPane(table);
line.setBounds(1, 117, 552, 313);
panel.add(line);
jl_id = new JLabel("学 号");
jl_id.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jl_id.setBounds(23, 63, 40, 25);
panel.add(jl_id);
jl_name = new JLabel("姓 名");
jl_name.setBounds(210, 63, 40, 25);
jl_name.setFont(new Font("微软雅黑", Font.PLAIN, 14));
panel.add(jl_name);
jtf_id = new JTextField();
jtf_id.setBounds(73, 63, 100, 25);
jtf_id.setColumns(10);
panel.add(jtf_id);
jtf_name = new JTextField();
jtf_name.setColumns(10);
jtf_name.setBounds(262, 63, 100, 25);
panel.add(jtf_name);
jb_seach = new JButton("搜 索");
jb_seach.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jb_seach.setBounds(403, 63, 80, 25);
panel.add(jb_seach);
jb_first = new JButton("首页");
jb_first.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jb_first.setBounds(10, 450, 65, 25);
panel.add(jb_first);
jb_up = new JButton("上页");
jb_up.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jb_up.setBounds(73, 450, 65, 25);
panel.add(jb_up);
jb_down = new JButton("下页");
jb_down.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jb_down.setBounds(136, 450, 65, 25);
panel.add(jb_down);
jb_last = new JButton("尾页");
jb_last.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jb_last.setBounds(197, 450, 65, 25);
panel.add(jb_last);
jft_go = new JTextField();
jft_go.setBounds(441, 453, 25, 20);
panel.add(jft_go);
jft_go.setColumns(10);
jb_jump = new JButton("跳转");
jb_jump.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jb_jump.setBounds(483, 450, 65, 25);
panel.add(jb_jump);
jl_di = new JLabel("第");
jl_di.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jl_di.setBounds(427, 450, 14, 25);
panel.add(jl_di);
jl_ye = new JLabel("页");
jl_ye.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jl_ye.setBounds(466, 450, 14, 25);
panel.add(jl_ye);
jl_gong = new JLabel("当前第 " + page + " 页 共 " + (count / limit + 1) + " 页");
jl_gong.setFont(new Font("微软雅黑", Font.PLAIN, 14));
jl_gong.setBounds(275, 451, 142, 25);
panel.add(jl_gong);
jb_seach.addActionListener(this);
jb_first.addActionListener(this);
jb_up.addActionListener(this);
jb_down.addActionListener(this);
jb_last.addActionListener(this);
jb_jump.addActionListener(this);
panel.setVisible(true);
return panel;
}
// List转为二维数组
private String[][] toArray(List<Student> stuList) {
String[][] result = new String[students.size()][7];
for (int i = 0; i < result.length; i++) {
result[i][0] = students.get(i).getNum();
result[i][1] = students.get(i).getName();
result[i][2] = students.get(i).getGender();
result[i][3] = students.get(i).getBirthday();
result[i][4] = "" + students.get(i).getAge();
result[i][5] = students.get(i).getMajor();
result[i][6] = students.get(i).getDorm();
}
return result;
}
// 设置表单列宽,每次更新完都有设置
private void setWidth() {
table.setRowHeight(29);
table.getColumnModel().getColumn(2).setPreferredWidth(40);
table.getColumnModel().getColumn(4).setPreferredWidth(40);
table.getColumnModel().getColumn(5).setPreferredWidth(110);
table.getColumnModel().getColumn(6).setPreferredWidth(110);
table.setRowSelectionAllowed(false);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb_seach) {
String num = jtf_id.getText();
String name = jtf_name.getText();
try {
session = new SqlSessionFactoryTool().getSqlSession();
mapper mapper = session.getMapper(mapper.class);
page = fristPage;
students = mapper.selectStuBySomething(num, name, (page - 1) * limit, limit);
count = mapper.countStu(num, name);
lastPage = count / limit + 1;
result = toArray(students);
jl_gong.setText("当前第 " + page + " 页 共 " + (count / limit + 1) + " 页");
stuTable.setDataVector(result, columnNames);
setWidth();
jft_go.setText("");
panel.repaint();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
session.close();
}
}
if (e.getSource() == jb_first) {
if (page == 1) {
JOptionPane.showMessageDialog(this, "当前为首页 !", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
String num = jtf_id.getText();
String name = jtf_name.getText();
try {
session = new SqlSessionFactoryTool().getSqlSession();
mapper mapper = session.getMapper(mapper.class);
page = fristPage;
students = mapper.selectStuBySomething(num, name, (page - 1) * limit, limit);
count = mapper.countStu(num, name);
lastPage = count / limit + 1;
result = toArray(students);
jl_gong.setText("当前第 " + page + " 页 共 " + (count / limit + 1) + " 页");
stuTable.setDataVector(result, columnNames);
setWidth();
panel.repaint();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
session.close();
}
}
}
if (e.getSource() == jb_up) {
if (page == 1) {
JOptionPane.showMessageDialog(this, "当前为首页 !", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
String num = jtf_id.getText();
String name = jtf_name.getText();
page = page - 1;
try {
session = new SqlSessionFactoryTool().getSqlSession();
mapper mapper = session.getMapper(map