第四:添加时的对话框
package com.sc.test3;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* 添加学生信息的对话框
* @author ZhangShaoWen
*
*/
public class StuAddDialog 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 StuAddDialog(Frame owner,String title,boolean model) {
//调用父类的构造函数,达到对话框效果
super(owner,title,model);
//初始化组件
jl1 = new JLabel("学号");
jl2 = new JLabel("姓名");
jl3 = new JLabel("性别");
jl4 = new JLabel("年龄");
jl5 = new JLabel("籍贯");
jl6 = new JLabel("系别");
jtf1 = new JTextField();
jtf2 = new JTextField();
jtf3 = new JTextField();
jtf4 = new JTextField();
jtf5 = new JTextField();
jtf6 = new JTextField();
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 = "insert into stu values(?,?,?,?,?,?)";
String []paras = {number,name,sex,age,nation,dept};
StuModel sm = new StuModel();
sm.updStu(sql, paras);
//关闭对话框
this.dispose();
}
}