第五:修改学生信息时的对话框
package com.sc.test3;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* 修改学生信息的对话框
* @author ZhangShaoWen
*
*/
public class StuUpDialog extends JDialog implements ActionListener{
//定义控件
JLabel jl1,jl2,jl3,jl4,jl5,jl6;
JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;
JButton jb1,jb2;
JPanel jp1,jp2,jp3;
public StuUpDialog(Frame owner,String title,boolean model,StuModel sm,int rowNums) {
//调用父类的构造函数,达到对话框效果
super(owner,title,model);
String paras[]={"1"};
sm.queryStu("select * from stu where 1=?", paras);
//初始化组件
jl1 = new JLabel("学号");
jl2 = new JLabel("姓名");
jl3 = new JLabel("性别");
jl4 = new JLabel("年龄");
jl5 = new JLabel("籍贯");
jl6 = new JLabel("系别");
jtf1 = new JTextField();
jtf1.setText((String)sm.getValueAt(rowNums, 0));
//主键不允许修改
jtf1.setEditable(false);
jtf2 = new JTextField();
jtf2.setText((String)sm.getValueAt(rowNums, 1));
jtf3 = new JTextField();
jtf3.setText((String)sm.getValueAt(rowNums, 2));
jtf4 = new JTextField();
jtf4.setText(sm.getValueAt(rowNums, 3).toString());
jtf5 = new JTextField();
jtf5.setText((String)sm.getValueAt(rowNums, 4));
jtf6 = new JTextField();
jtf6.setText((String)sm.getValueAt(rowNums, 5));
jb1 = new JButton("修改");
jb1.addActionListener(this);//注册监听
jb2 = new JButton("取消");
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
//设置布局
jp1.setLayout(new GridLayout(6,1));
jp2.setLayout(new GridLayout(6,1));
//添加组件
jp1.add(jl1);
jp1.add(jl2);
jp1.add(jl3);
jp1.add(jl4);
jp1.add(jl5);
jp1.add(jl6);
jp2.add(jtf1);
jp2.add(jtf2);
jp2.add(jtf3);
jp2.add(jtf4);
jp2.add(jtf5);
jp2.add(jtf6);
jp3.add(jb1);
jp3.add(jb2);
this.add(jp1,BorderLayout.WEST);
this.add(jp2,BorderLayout.CENTER);
this.add(jp3,BorderLayout.SOUTH);
this.setSize(300,250);
this.setVisible(true);
}
//监听器
@Override
public void actionPerformed(ActionEvent e) {
String number = jtf1.getText();
String name = jtf2.getText();
String sex = jtf3.getText();
String age = jtf4.getText().toString();
String nation = jtf5.getText();
String dept = jtf6.getText();
String sql = "update stu set stuName=?,stuSex=?,stuAge=?,stuJg=?,stuDept=? where stuId=?";
String []paras = {name,sex,age,nation,dept,number};
StuModel sm = new StuModel();
sm.updStu(sql, paras);
//关闭对话框
this.dispose();
}
}