package com.refer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.sql.Connection;
public class MainForm extends JFrame implements ActionListener{
public static JTable jt;
JScrollPane jsp;
JLabel jl1;
JButton findbtn,updatebtn,delbtn,insertbtn,showPagebtn;
JButton Upbtn,Downbtn;
JTextField jtf;
public static MyModelTable mmt;
public static String stuId = null;
public static Connection con = null;
sqlHelper sqlhelper = new sqlHelper();
/*public String getStuId() {
return stuId;
}
public void setStuId(String stuId) {
this.stuId = stuId;
}*/
public MainForm()
{
this.setLayout(new FlowLayout());
jl1 = new JLabel("请输入学生学号:");
jtf = new JTextField(10);
findbtn = new JButton("查询");
findbtn.addActionListener(this);
showPagebtn = new JButton("分页显示");
showPagebtn.addActionListener(this);
this.add(jl1);
this.add(jtf);
this.add(findbtn);
this.add(showPagebtn);
Upbtn = new JButton("上一页");
Upbtn.addActionListener(this);
Upbtn.setVisible(false);
insertbtn = new JButton("插入");
insertbtn.addActionListener(this);
updatebtn = new JButton("修改");
updatebtn.addActionListener(this);
delbtn = new JButton("删除");
delbtn.addActionListener(this);
Downbtn = new JButton("下一页");
Downbtn.addActionListener(this);
Downbtn.setVisible(false);
mmt = new MyModelTable();
jt = new JTable(mmt);
jt.setBackground(Color.pink);
jt.setSize(200,100);
jsp = new JScrollPane(jt);
this.add(jsp);
this.add(Upbtn);
this.add(insertbtn);
this.add(updatebtn);
this.add(delbtn);
this.add(Downbtn);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("学生管理系统");
this.setLocation(100,200);
this.setSize(480,530);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource()==findbtn)
{
stuId = jtf.getText().trim();
String sql;
if(stuId.equals(""))
{
sql = "select * from student";
}
else{
sql = "select * from student where stuId='"+stuId+"'";
}
mmt = new MyModelTable(sql);
jt.setModel(mmt);
}
if(arg0.getSource()==showPagebtn)
{
Upbtn.setVisible(true);
Downbtn.setVisible(true);
//ConnectionFactory con = new ConnectionFactory();
String sql=null;
try {
sql = sqlhelper.showPageData();
mmt = new MyModelTable(sql);
jt.setModel(mmt);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(arg0.getSource()==insertbtn)
{
//InsertDataForm form = new InsertDataForm();
StuAddDiag form = new StuAddDiag(this,"添加学生信息", true);
//form.show();
mmt = new MyModelTable();
jt.setModel(mmt);
}
if(arg0.getSource()==updatebtn)
{
int rowNum = this.jt.getSelectedRow();
if(rowNum==-1)
{
JOptionPane.showMessageDialog(this, "请选择修改行");
return;
}
UpdStuDialog form = new UpdStuDialog(this, "修改学生信息", true, mmt, rowNum);
mmt = new MyModelTable();
jt.setModel(mmt);
}
if(arg0.getSource()==delbtn)
{
DelStuDialog form = new DelStuDialog(this,"删除学生信息", true);
mmt = new MyModelTable();
jt.setModel(mmt);
}
if(arg0.getSource()==Upbtn)
{
sqlHelper.pageNow--;
if(sqlHelper.pageNow==0)
{
JOptionPane.showMessageDialog(this, "已经是第一页了");
sqlHelper.pageNow++;
}
//ConnectionFactory con = new ConnectionFactory();
String sql=null;
try {
sql = sqlhelper.showPageData();
mmt = new MyModelTable(sql);
jt.setModel(mmt);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(arg0.getSource()==Downbtn)
{
sqlHelper.pageNow++;
if(sqlHelper.pageNow>sqlHelper.pageCount)
{
JOptionPane.showMessageDialog(this, "已经是最后一页了");
sqlHelper.pageNow--;
}
//ConnectionFactory con = new ConnectionFactory();
String sql=null;
try {
sql = sqlhelper.showPageData();
mmt = new MyModelTable(sql);
jt.setModel(mmt);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}