import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import java.util.Vector;
public class AdmPanel extends JPanel implements ActionListener,ItemListener,MouseListener
{
private static final long serialVersionUID = 1L;//添加默认序列号
JButton b1,b2,b3,b4,b5,b6;
JPanel p1,p2,pCenter;
CardLayout card=null;
JTextField t1=new JTextField(10),
t2=new JTextField(10),
t3=new JTextField(10),
tt1=new JTextField(10),
tt2=new JTextField(10),
tt3=new JTextField(10);
ButtonGroup sex1=new ButtonGroup(),
sex2=new ButtonGroup();
JRadioButton radio1=new JRadioButton("男",true),
radio2=new JRadioButton("女");
JRadioButton r1=new JRadioButton("男",true),
r2=new JRadioButton("女");
JComboBox year,month,date,yy,mm,dd,post;
JTable table1,table2;
Vector<String> vectorColName1=new Vector<String>(),
vectorColName2=new Vector<String>();
Vector<Vector<String>> vector1=new Vector<Vector<String>>(),
vector2=new Vector<Vector<String>>();
DefaultTableModel model1=new DefaultTableModel(vectorColName1,0){
private static final long serialVersionUID = 1L;//添加默认序列号
public boolean isCellEditable(int row, int column)
{
return false;
}
};
DefaultTableModel model2=new DefaultTableModel(vectorColName2,0){
private static final long serialVersionUID = 1L;//添加默认序列号
public boolean isCellEditable(int row, int column)
{
return false;
}
};
AdmPanel()
{
setLayout(new BorderLayout());
t1=new JTextField(10);
t2=new JTextField(10);
t3=new JTextField(10);
tt1=new JTextField(10);
tt2=new JTextField(10);
tt3=new JTextField(10);
sex1.add(radio1);
sex1.add(radio2);
sex2.add(r1);
sex2.add(r2);
year=new JComboBox();
month=new JComboBox();
date=new JComboBox();
yy=new JComboBox();
mm=new JComboBox();
dd=new JComboBox();
post=new JComboBox();
post.addItem("助教");
post.addItem("讲师");
post.addItem("副教授");
post.addItem("教授");
for(int i=1980;i<1995;i++)
year.addItem(i);
for(int i=1;i<=12;i++)
month.addItem(i);
for(int i=1;i<=31;i++)
date.addItem(i);
for(int i=1950;i<1995;i++)
yy.addItem(i);
for(int i=1;i<=12;i++)
mm.addItem(i);
for(int i=1;i<=31;i++)
dd.addItem(i);
year.addItemListener(this);
month.addItemListener(this);
yy.addItemListener(this);
mm.addItemListener(this);
post.addItemListener(this);
b1=new JButton("学生信息管理");
b2=new JButton("教师信息管理");
b3=new JButton("输入");
b4=new JButton("删除");
b5=new JButton("输入");
b6=new JButton("删除");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
JPanel p0=new JPanel();
p0.add(b1);
p0.add(b2);
pCenter=new JPanel();
card=new CardLayout();
pCenter.setLayout(card);
p1=createStuPanel();
p2=createTeaPanel();
pCenter.add("学生信息管理",p1);
pCenter.add("教师信息管理",p2);
add(p0,BorderLayout.NORTH);
add(pCenter,BorderLayout.CENTER);
}
private JPanel createStuPanel()
{
JPanel p=new JPanel();
JScrollPane p1;
JPanel p2=new JPanel();
p.setLayout(new GridLayout(2,1));
SqlManager DBm=SqlManager.createInstance();//单态模式获取实例
DBm.connectDB();
String sql="exec ProcAllStu";
ResultSet rs=DBm.executeQuery(sql);
vectorColName1.addElement("学号");
vectorColName1.addElement("姓名");
vectorColName1.addElement("性别");
vectorColName1.addElement("生日");
vectorColName1.addElement("班级");
model1.setDataVector(vector1,vectorColName1);
table1=new JTable(model1);
table1.addMouseListener(this);
p1=new JScrollPane(table1);
p.add(p1);
try{
while(rs.next()){
Vector<String> rec_vector=new Vector<String>(); //从结果集中取数据放入向量rec_vector中
rec_vector.addElement(rs.getString(1));
rec_vector.addElement(rs.getString(2));
rec_vector.addElement(rs.getString(3));
rec_vector.addElement(rs.getString(4));
rec_vector.addElement(rs.getString(5));
vector1.addElement(rec_vector); //向量rec_vector加入向量vect中
}
//tm.fireTableStructureChanged();//更新表格,显示向量vect的内容
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
DBm.closeDB();
//构造p2
Box base=Box.createHorizontalBox(),
boxleft=Box.createVerticalBox(),
boxright=Box.createVerticalBox();
JPanel pp1=new JPanel(),
pp2=new JPanel(),
pp3=new JPanel(),
pp4=new JPanel(),
pp5=new JPanel(),
pp6=new JPanel(),
psex=new JPanel(),
pbirth=new JPanel();
psex.add(radio1);
psex.add(radio2);
pbirth.add(year);
pbirth.add(month);
pbirth.add(date);
pp1.add(new JLabel("学生编号:"));
pp1.add(t1);
pp2.add(new JLabel("学生姓名:"));
pp2.add(t2);
pp3.add(new JLabel("学生性别:"));
pp3.add(psex);
pp4.add(new JLabel("学生生日:"));
pp4.add(pbirth);
pp5.add(new JLabel("所在班级:"));
pp5.add(t3);
pp6.add(b3);
pp6.add(b4);
boxleft.add(Box.createVerticalStrut(30));
boxleft.add(pp1);
boxleft.add(Box.createVerticalStrut(20));
boxleft.add(pp2);
boxleft.add(Box.createVerticalStrut(20));
boxleft.add(pp3);
boxright.add(Box.createVerticalStrut(20));
boxright.add(pp4);
boxright.add(Box.createVerticalStrut(20));
boxright.add(pp5);
boxright.add(Box.createVerticalStrut(20));
boxright.add(pp6);
base.add(boxleft);
base.add(Box.createHorizontalStrut(80));
base.add(boxright);
p2.add(base);
p.add(p2);
return p;
}
private JPanel createTeaPanel()
{
JPanel p=new JPanel();
JScrollPane p1;
JPanel p2=new JPanel();
p.setLayout(new GridLayout(2,1));
SqlManager DBm=SqlManager.createInstance(); //单态模式获取实例
DBm.connectDB();
String sql="exec ProcAllTea";
ResultSet rs=DBm.executeQuery(sql);
vectorColName2.addElement("教师编号");
vectorColName2.addElement("教师姓名");
vectorColName2.addElement("教师性别");
vectorColName2.addElement("生日");
vectorColName2.addElement("职称");
vectorColName2.addElement("所在院系");
model2.setDataVector(vector2,vectorColName2);
table2=new JTable(model2);
table2.addMouseListener(this);
p1=new JScrollPane(table2);
p.add(p1);
try{
while(rs.next()){
Vector<String> rec_vector=new Vector<String>(); //从结果集中取数据放入向量rec_vector中
rec_vector.addElement(rs.getString(1));
rec_vector.addElement(rs.getString(2));
rec_vector.addElement(rs.getString(3));
rec_vector.addElement(rs.getString(4));
rec_vector.addElement(rs.getString(5));
rec_vector.addElement(rs.getString(6));
vector2.addElement(rec_vector); //向量rec_vector加入向量vect中
}
//tm.fireTableStructureChanged();//更新表格,显示向量vect的内容
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
DBm.closeDB();
//构造p2
Box base=Box.createHorizontalBox(),
boxleft=Box.createVerticalBox(),
boxright=Box.createVerticalBox();
JPanel pp1=new JPanel(),
pp2=new JPanel(),
pp3=new JPanel(),
pp4=new JPanel(),
pp5=new JPanel(),
pp6=new JPanel(),
pp7=new JPanel(),