package com.student.show;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import com.student.add.Add;
import com.student.check.Check;
import com.student.dao.StuDAO;
import com.student.delete.Delete;
import com.student.update.Update;
import com.student.vo.Student;
public class Show extends JFrame {
public static int pagesize=5;//每页显示5条信息
public static int pagenow=1;//当前页为第一页
public Show() {
setSize(500, 430);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);//点X号就是关闭
setResizable(false);//不可改变窗口大小
setLocationRelativeTo(null);//默认居中显示
setLayout(null);//采用坐标布局
StuDAO dao = new StuDAO();//前面我们已经把增删改查分页写到StuDAO里面,现在就直接拿出来用
List<Student> list =dao.findPage(pagesize, pagenow);
Student stu = new Student();
for (int i = 0; i < list.size(); i++) {
stu = list.get(i);
}
String[] rowName = { "学号", "姓名", "年龄", "性别" };//从这里开始是二维数组的遍历使用
Object[][] data = new Object[list.size()][4];
for (int i = 0; i < list.size(); i++) {
Student s = list.get(i);
Object st[] = { s.getStuid(), s.getName(), s.getAge(), s.getSex() };
data[i] = st;
}
final JTable table = new JTable(data,rowName);
JScrollPane JSP=new JScrollPane(table);//这一步不能省去,否则显示不出列名
JSP.setBounds(20, 10, 400, 200);
add(JSP);
JButton jb11=new JButton("首页");
jb11.setBounds(40,220,80,30);
add(jb11);
JButton jb22=new JButton("上一页");
jb22.setBounds(130,220,80,30);
add(jb22);
JButton jb33=new JButton("下一页");
jb33.setBounds(220,220,80,30);
add(jb33);
JButton jb44=new JButton("尾页");
jb44.setBounds(310,220,80,30);
add(jb44);
JButton jb1 = new JButton("查看信息");
jb1.setBounds(50, 270, 100, 30);
add(jb1);
JButton jb2 = new JButton("修改信息");
jb2.setBounds(280, 270, 100, 30);
add(jb2);
JButton jb3 = new JButton("添加信息");
jb3.setBounds(50, 320, 100, 30);
add(jb3);
JButton jb4 = new JButton("删除信息");
jb4.setBounds(280, 320, 100, 30);
add(jb4);
// JButton jb5 = new JButton("退出");
// jb5.setBounds(280, 360, 100, 30);
// add(jb5);
jb1.addActionListener(new ActionListener() {//查看
public void actionPerformed(ActionEvent event) {
int row = table.getSelectedRow();//选中第几行
int index = 0;
if(row==-1){
JOptionPane.showMessageDialog(null,"您没有选中信息");
return;
}
String id = String.valueOf(table.getValueAt(row, index));// 跟Check联系起来
System.out.println(id);
Check check=new Check(id);
check.setVisible(true);
setVisible(false);
}
});
jb2.addActionListener(new ActionListener() {//修改
public void actionPerformed(ActionEvent event) {
int row = table.getSelectedRow();
int index = 0;
if(row==-1){
JOptionPane.showMessageDialog(null,"您没有选中信息");
return;
}
String id = String.valueOf(table.getValueAt(row, index));// 跟Update联系起来
Update up=new Update(id);
up.setVisible(true);
setVisible(false);
}
});
jb3.addActionListener(new ActionListener() {//添加
public void actionPerformed(ActionEvent event) {
Add add = new Add();
add.setVisible(true);
setVisible(false);
}
});
jb4.addActionListener(new ActionListener() {//删除
public void actionPerformed(ActionEvent event) {
int row = table.getSelectedRow();
int index = 0;
if(row==-1){
JOptionPane.showMessageDialog(null,"您没有选中信息");
return;
}
String num=String.valueOf(table.getValueAt(row, index));
Delete d=new Delete(num);
d.setVisible(true);
setVisible(false);
}
});
jb11.addActionListener(new ActionListener() {//首页
public void actionPerformed(ActionEvent event) {
pagenow=1;
Show show=new Show();
setVisible(false);
show.setVisible(true);
}
});
jb22.addActionListener(new ActionListener() {//上一页
public void actionPerformed(ActionEvent event) {
if(pagenow != 1){
pagenow=pagenow-1;
}else{
return;
}
Show show=new Show();
setVisible(false);
show.setVisible(true);
}
});
jb33.addActionListener(new ActionListener() {//下一页
public void actionPerformed(ActionEvent event) {
StuDAO dao=new StuDAO();
int count=dao.findcount();
int pageCount=(count-1)/pagesize+1;//pageCount表示最后一页
if(pagenow != pageCount){
pagenow=pagenow+1;
}else{
return;
}
Show show=new Show();
setVisible(false);
show.setVisible(true);
}
});
jb44.addActionListener(new ActionListener() {//尾页
public void actionPerformed(ActionEvent event) {
StuDAO dao=new StuDAO();
int count=dao.findcount();
int pageCount=(count-1)/pagesize+1;
pagenow=pageCount;
Show show=new Show();
setVisible(false);
show.setVisible(true);
}
});
}
public static void main(String args[]) {
Show s = new Show();
}
}
- 1
- 2
- 3
前往页