问题是这样的:
在第一次点击添加按钮
弹出子窗口时,数据都是正常录入的
并且能添加到主窗口的表格和数据库中但是关闭这个子窗口
再重新打开
就出现了问题了,这次数据自己录入了两次,就是我输入的值录入一次,然后程序自己自个又录入一次当我再次关闭这个子窗口,又重新打开
这次我输入一次,程序多重复录入两次!!!程序自己重复录入的次数和我关闭再打开的次数有关!!!如果第一次打开,我输入一次,点击确认按钮一次,程序总是正常的,应该不是监听器释放的问题,而关闭一个窗口,应该是将其所有资源都释放了为什么和和子窗口的打开关闭次数有关???
代码如下: 

解决方案 »

  1.   

    package userManage;//import classAddress;
    import kunpeng.loginWindow.*;import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.event.*;
    import java.sql.*;
    import java.util.Vector;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;public class UserManage extends JFrame {
    ResultSet rs=null;
    Statement stmt;
    Connection conn;
    //子窗口输入框
    JTextField userName =new JTextField(12);
    JPasswordField password=new JPasswordField(12);
    JPasswordField confirmPassword=new JPasswordField(12);
    //子窗口标签
    JLabel userNameLabel =new JLabel("新用户:");
    //userNameLabel.setFont(new Font("微软雅黑",Font.BOLD,18));
    JLabel passwordLabel =new JLabel("密码:");
    JLabel confirmPasswordLabel=new JLabel("确认密码:");
    JLabel userRightSetLabel=new JLabel("权限:");

    //复选框
    JComboBox userRightSet=new JComboBox();
    //子窗口JFrame
    JFrame add_User_Window =new JFrame();//增加用户窗口
    JFrame addUser_Notice=new JFrame();//增加用户窗口的各种提示信息


    //从数据库获取数据方法     
    public Vector  getData(){  
    Vector vector=new Vector();
    try {
    Class.forName("com.mysql.jdbc.Driver");//驱动
    Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/KunPengContactsDataBase?useUnicode=true&characterEncoding=gbk","root","3508522"); //陈述对象
    java.sql.Statement stmt=conn.createStatement();
        
        
        String createDataBase="CREATE DATABASE IF NOT EXISTS KunPengContactsDataBase ";//创建数据库
        String createTable="CREATE TABLE KunPengContactsDataBase(id int unsigned NOT NULL auto_increment,userName varchar(20) NOT NULL,password" +
                     " varchar(20) NOT NULL,userRight varchar(100) NOT NULL,PRIMARY KEY(id))";//创建表
        String dropTable="DROP TABLE IF EXISTS KunPengContactsDataBase";//判断表是否存在
        
        stmt.execute(createDataBase);
        stmt.execute(dropTable);
        stmt.execute(createTable);


    //返回结果集
    rs=stmt.executeQuery("select * from KunPengContactsDataBase");
    ResultSetMetaData rm=rs.getMetaData();

    //循环打印出数据库表中数据
    int n=rm.getColumnCount();
    while(rs.next()){   
    Vector ve=new Vector();
    for(int i=1;i<n+1;i++){
    //System.out.print(rs.getString(i)+"  ");
    String s=rs.getString(i);
    ve.add(s);
    }
    vector.add(ve);//Vector对象接收表中数据
    }
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{if(rs!=null){
    try {
    rs.close();//关闭结果集
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if(stmt!=null){
    try {
    stmt.close();//关闭陈述对象
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if(conn!=null){
    try {
    conn.close();//关闭连接
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    return vector;
    }
    //向数据库插入数据方法
    public void insertData(String user,String passwordText,String userRightSet_select){   
    try {

    Class.forName("com.mysql.jdbc.Driver");//驱动
        Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/KunPengContactsDataBase?useUnicode=true&characterEncoding=gbk","root","3508522");//建立连接
        
        
        String createDataBase="CREATE DATABASE IF NOT EXISTS KunPengContactsDataBase";//创建数据库
        String createTable="CREATE TABLE KunPengContactsDataBase(id int unsigned NOT NULL auto_increment,userName varchar(20) NOT NULL,password" +
                     " varchar(20) NOT NULL,userRight varchar(100) NOT NULL,PRIMARY KEY(id))";//创建表
        String dropTable="DROP TABLE IF EXISTS KunPengContactsDataBase";//判断表是否存在
        String selectData="SELECT * FROM KunPengContactsDataBase where id=1";
        //String code="SET KunPengContactsDatabase GBK";

    stmt=conn.createStatement();
    //stmt.execute(code);

     int c=stmt.executeUpdate("insert into KunPengContactsDataBase(userName,password,userRight) values('" + user + "','" +passwordText + "','"+userRightSet_select+"')");
     ResultSet result=stmt.executeQuery(selectData);
     
     while (result.next()){
     System.out.println(result.getString("userRight")+"");
     }
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }  public void deleteUser() throws ClassNotFoundException, SQLException
     {
     Class.forName("com.mysql.jdbc.Driver");//驱动
        Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/KunPengContactsDataBase" +
         "?useUnicode=true&characterEncoding=gbk","root","3508522");//建立连接
        
        String deleteUser="";
     

     }
      

  2.   

    public  UserManage(){ 
    //界面参数设置               
    this.setTitle("用户设置");
    this.setBounds(400, 300, 500, 300);
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    userRightSet.addItem("普通用户");
    userRightSet.addItem("游客");
    JButton Add_User;

    Vector vector1=new Vector();
    vector1.add("用户名");
    vector1.add("权限");

    //默认表格模型
    final DefaultTableModel tableModel=new DefaultTableModel();
    tableModel.setDataVector(getData(), vector1);
    JTable table=new JTable(tableModel);
    table.getSelectedRow();
    /*添加点击事件*/
    table.addMouseListener(new MouseListener(){ @Override
    public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub
    JOptionPane.showMessageDialog(addUser_Notice,
        "用户名不能为空",
        "提示",
        JOptionPane.WARNING_MESSAGE);


    } @Override
    public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub


    } @Override
    public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

    } @Override
    public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

    } @Override
    public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

    }

    });

    Add_User=new JButton("增加用户");
    JButton Edit_User=new JButton("编辑用户");
    JButton Del_User=new JButton("删除用户");

    //添加一个面板并设置参数
    JPanel jp=new JPanel();
    jp.setBackground(Color.darkGray);

    //向面板中添加组件
        //jp.add(label1);
    jp.add(Add_User);
    jp.add(Edit_User);
    jp.add(Del_User);
    //jp.add(button);

    //将表格添加到滚动面板
    JScrollPane js=new JScrollPane(table);

    //向JFrame添加组件
    this.getContentPane().add(js);
    this.add(jp,BorderLayout.SOUTH);
    this.setVisible(true);

    //添加用户  按钮事件绑定
    Add_User.addActionListener(new ActionListener()
    {

    String userRightSet_select="普通用户";
    String passwordText;
    String confirmPasswordText;
    String userRightSet_selectText;
    String user;
    JButton yesButton=new JButton("确认");
    JButton cancelButton=new JButton("取消");
    Box LabelBox,FieldBox,LabelFieldBox,ButtonBox,BaseBox;//使用BoxLayout

    //子窗口按钮


    @Override
    public void actionPerformed(ActionEvent e) {
    /*标签Box*/
    LabelBox=Box.createVerticalBox();
    LabelBox.add(userNameLabel);
    LabelBox.add(Box.createVerticalStrut(12));
    LabelBox.add(passwordLabel);
    LabelBox.add(Box.createVerticalStrut(12));
    LabelBox.add(confirmPasswordLabel);
    LabelBox.add(Box.createVerticalStrut(12));
    LabelBox.add(userRightSetLabel);

    /*输入框Box*/
    FieldBox=Box.createVerticalBox();
    FieldBox.add(Box.createVerticalStrut(10));
    FieldBox.add(userName);
    FieldBox.add(Box.createVerticalStrut(10));
    FieldBox.add(password);
    FieldBox.add(Box.createVerticalStrut(10));
    FieldBox.add(confirmPassword);
    FieldBox.add(Box.createVerticalStrut(10));
    FieldBox.add(userRightSet);

    /*按钮Box*/
    ButtonBox=Box.createHorizontalBox();
    ButtonBox.add(Box.createHorizontalStrut(120));
    ButtonBox.add(yesButton);
    ButtonBox.add(Box.createHorizontalStrut(20));
    ButtonBox.add(cancelButton);

    /*标签和输入框Box*/
    LabelFieldBox=Box.createHorizontalBox();
    LabelFieldBox.add(Box.createHorizontalStrut(20));
    LabelFieldBox.add(LabelBox);
    LabelFieldBox.add(Box.createHorizontalStrut(10));
    LabelFieldBox.add(FieldBox);
    LabelFieldBox.add(Box.createHorizontalStrut(20));

    /*baseBox*/
    BaseBox=Box.createVerticalBox();
    BaseBox.add(Box.createVerticalStrut(12));
    BaseBox.add(LabelFieldBox);
    BaseBox.add(Box.createVerticalStrut(8));
    BaseBox.add(ButtonBox);
    BaseBox.add(Box.createVerticalStrut(10));

    add_User_Window.add(BaseBox);
        add_User_Window.setBounds(120,125,308,220);
    add_User_Window.setBackground(Color.blue);
    add_User_Window.setResizable(false);
    add_User_Window.show();

                
    //复合框事件监听器
    userRightSet.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e) 
    {
    // TODO Auto-generated method stub
    JComboBox cb=(JComboBox)e.getSource();
    userRightSet_select=(String)cb.getSelectedItem();

    }

    });
    /*确定 按钮事件*/
    yesButton.addActionListener(new ActionListener()
    {
    @Override
    public void actionPerformed(ActionEvent e) 
    {
    // TODO Auto-generated method stub
        //获得输入框中的值

    passwordText=new String(password.getText().toString());
    confirmPasswordText=new String(confirmPassword.getText().toString());
    userRightSet_selectText=userRightSet_select;
    user=new String(userName.getText().toString());

    System.out.println(user+"daozhelima");
    System.out.println(passwordText);


    if(!user.equals("")&&!passwordText.equals("")&&passwordText.equals(confirmPasswordText))
    {

    insertData(user,passwordText,userRightSet_selectText);//插入数据
    System.out.println("到这里");

    System.out.println(user);

    Vector v2=new Vector();//接受数据并显示在列表中
    v2.add(user);
    System.out.println(user+"lalalalalalalal");
    v2.add(userRightSet_selectText);
    tableModel.addRow(v2);

    JOptionPane.showMessageDialog(addUser_Notice,
        "添加用户成功",
        "温馨提示",
        JOptionPane.INFORMATION_MESSAGE);
    }else{
    JOptionPane.showMessageDialog(addUser_Notice,
        "添加用户失败",
        "温馨提示",
        JOptionPane.INFORMATION_MESSAGE);

    }
    //userName.setText(null);
    }
    });
    cancelButton.addActionListener(new ActionListener(){ @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    add_User_Window.dispose();

    }

    });

    }

    });

    }                                                                                                                                                                                                                                                                                                                                                                                                                                
    public static void main(String[]args){
    UserManage add=new UserManage();
    add.show();
    }
    }                                                                                              
      

  3.   

    我调试了一下,发现添加用户窗口在第一次关闭后再打开,按一次确认的Button会执行两次Button事件,感觉有点不可思议。楼主还是试试把添加用户的窗口做为一个内部类试试,像你现在写的这样感觉有点混乱,那个匿名类写得太夸张了,还是将它改为内部类吧,也许这样会管用点。下面是我改写的内部类方法,但还有有问题,就是重新打开后会出现两个确认按钮和两个取消按钮,但是已经没有原来那种输入多次的问题了,楼主还是自己再改改吧
      

  4.   


    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.event.*;
    import java.sql.*;
    import java.util.Vector;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;public class UserManage extends JFrame 
    {
    ResultSet rs=null;
    Statement stmt;
    Connection conn;
    //子窗口输入框
    JTextField userName =new JTextField(12);
    JPasswordField password=new JPasswordField(12);
    JPasswordField confirmPassword=new JPasswordField(12);
    //子窗口标签
    JLabel userNameLabel =new JLabel("新用户:");
    //userNameLabel.setFont(new Font("微软雅黑",Font.BOLD,18));
    JLabel passwordLabel =new JLabel("密码:");
    JLabel confirmPasswordLabel=new JLabel("确认密码:");
    JLabel userRightSetLabel=new JLabel("权限:");

    //复选框
    JComboBox userRightSet=new JComboBox();
    //子窗口JFrame
    JFrame add_User_Window =new JFrame();//增加用户窗口
    JFrame addUser_Notice=new JFrame();//增加用户窗口的各种提示信息

    DefaultTableModel tableModel;

    //从数据库获取数据方法
    public Vector getData()
    {
    Vector vector=new Vector();
    try 
    {
    Class.forName("com.mysql.jdbc.Driver");//驱动
    Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/KunPengContactsDataBase?useUnicode=true&characterEncoding=gbk","root","3508522");
    //陈述对象
    java.sql.Statement stmt=conn.createStatement();
    String createDataBase="CREATE DATABASE IF NOT EXISTS KunPengContactsDataBase ";//创建数据库
    String createTable="CREATE TABLE KunPengContactsDataBase(id int unsigned NOT NULL auto_increment,userName varchar(20) NOT NULL,password" +
    " varchar(20) NOT NULL,userRight varchar(100) NOT NULL,PRIMARY KEY(id))";//创建表
    String dropTable="DROP TABLE IF EXISTS KunPengContactsDataBase";//判断表是否存在

    stmt.execute(createDataBase);
    stmt.execute(dropTable);
    stmt.execute(createTable);

    //返回结果集
    rs=stmt.executeQuery("select * from KunPengContactsDataBase");
    ResultSetMetaData rm=rs.getMetaData();

    //循环打印出数据库表中数据
    int n=rm.getColumnCount();
    while(rs.next())
    {
    Vector ve=new Vector();
    for(int i=1;i<n+1;i++)
    {
    //System.out.print(rs.getString(i)+" ");
    String s=rs.getString(i);
    ve.add(s);
    }
    vector.add(ve);//Vector对象接收表中数据
    }
    }
    catch (Exception e) 
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    finally
    {
    if(rs!=null)
    {
    try
    {
    rs.close();//关闭结果集
    }
    catch (Exception e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if(stmt!=null)
    {
    try
    {
    stmt.close();//关闭陈述对象
    }
    catch (Exception e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if(conn!=null)
    {
    try
    {
    conn.close();//关闭连接
    }
    catch (Exception e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    return vector;
    } //向数据库插入数据方法
    public void insertData(String user,String passwordText,String userRightSet_select)
    {
    try 
    {
    Class.forName("com.mysql.jdbc.Driver");//驱动
    Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/KunPengContactsDataBase?useUnicode=true&characterEncoding=gbk","root","3508522");//建立连接
    String createDataBase="CREATE DATABASE IF NOT EXISTS KunPengContactsDataBase";//创建数据库
    String createTable="CREATE TABLE KunPengContactsDataBase(id int unsigned NOT NULL auto_increment,userName varchar(20) NOT NULL,password" +
    " varchar(20) NOT NULL,userRight varchar(100) NOT NULL,PRIMARY KEY(id))";//创建表
    String dropTable="DROP TABLE IF EXISTS KunPengContactsDataBase";//判断表是否存在
    String selectData="SELECT * FROM KunPengContactsDataBase where id=1";
    //String code="SET KunPengContactsDatabase GBK";

    stmt=conn.createStatement();
    //stmt.execute(code);
    int c=stmt.executeUpdate("insert into KunPengContactsDataBase(userName,password,userRight) values('" + user + "','" +passwordText + "','"+userRightSet_select+"')");
    ResultSet result=stmt.executeQuery(selectData);
    while (result.next())
    {
    System.out.println(result.getString("userRight")+"");
    }
    }
    catch (Exception e) 
    {
    e.printStackTrace();
    }
    }

    public void deleteUser() throws ClassNotFoundException, SQLException
    {
    Class.forName("com.mysql.jdbc.Driver");//驱动
    Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/KunPengContactsDataBase" +
    "?useUnicode=true&characterEncoding=gbk","root","3508522");//建立连接
    String deleteUser="";
    }

    public UserManage()
    {
    //界面参数设置
    this.setTitle("用户设置");
    this.setBounds(400, 300, 500, 300);
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    userRightSet.addItem("普通用户");
    userRightSet.addItem("游客");
    JButton Add_User;

    Vector vector1=new Vector();
    vector1.add("用户名");
    vector1.add("权限");

    //默认表格模型
    //final DefaultTableModel tableModel=new DefaultTableModel();
    tableModel = new DefaultTableModel();
    tableModel.setDataVector(getData(), vector1);
    JTable table=new JTable(tableModel);
    table.getSelectedRow();

    /*添加点击事件*/
    table.addMouseListener(new MouseListener()
    {
    @Override
    public void mouseClicked(MouseEvent arg0) 
    {
    // TODO Auto-generated method stub
    JOptionPane.showMessageDialog(addUser_Notice,
    "用户名不能为空",
    "提示",
    JOptionPane.WARNING_MESSAGE);
    }

    @Override
    public void mouseEntered(MouseEvent arg0) 
    {
    // TODO Auto-generated method stub
    }

    @Override
    public void mouseExited(MouseEvent arg0) 
    {
    // TODO Auto-generated method stub
    } @Override
    public void mousePressed(MouseEvent arg0) 
    {
    // TODO Auto-generated method stub
    } @Override
    public void mouseReleased(MouseEvent arg0) 
    {
    // TODO Auto-generated method stub
    }
    });

    Add_User=new JButton("增加用户");
    JButton Edit_User=new JButton("编辑用户");
    JButton Del_User=new JButton("删除用户"); //添加一个面板并设置参数
    JPanel jp=new JPanel();
    jp.setBackground(Color.darkGray);

    //向面板中添加组件
    //jp.add(label1);
    jp.add(Add_User);
    jp.add(Edit_User);
    jp.add(Del_User);
    //jp.add(button);

    //将表格添加到滚动面板
    JScrollPane js=new JScrollPane(table);

    //向JFrame添加组件
    this.getContentPane().add(js);
    this.add(jp,BorderLayout.SOUTH);
    this.setVisible(true);

    //添加用户 按钮事件绑定
    Add_User.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    new AddUser();
    }
    });
    } public static void main(String[]args)
    {
    UserManage add=new UserManage();
    add.show();
    }
      


      

  5.   

    内部类:private class AddUser
    {
    String userRightSet_select="普通用户";
    String passwordText;
    String confirmPasswordText;
    String userRightSet_selectText;
    String user;
    JButton yesButton=new JButton("确认");
    JButton cancelButton=new JButton("取消");
    Box LabelBox,FieldBox,LabelFieldBox,ButtonBox,BaseBox;//使用BoxLayout

    //子窗口按钮
    public AddUser()
    {
    /*标签Box*/
    LabelBox=Box.createVerticalBox();
    LabelBox.add(userNameLabel);
    LabelBox.add(Box.createVerticalStrut(12));
    LabelBox.add(passwordLabel);
    LabelBox.add(Box.createVerticalStrut(12));
    LabelBox.add(confirmPasswordLabel);
    LabelBox.add(Box.createVerticalStrut(12));
    LabelBox.add(userRightSetLabel);

    /*输入框Box*/
    FieldBox=Box.createVerticalBox();
    FieldBox.add(Box.createVerticalStrut(10));
    FieldBox.add(userName);
    FieldBox.add(Box.createVerticalStrut(10));
    FieldBox.add(password);
    FieldBox.add(Box.createVerticalStrut(10));
    FieldBox.add(confirmPassword);
    FieldBox.add(Box.createVerticalStrut(10));
    FieldBox.add(userRightSet); /*按钮Box*/
    ButtonBox=Box.createHorizontalBox();
    ButtonBox.add(Box.createHorizontalStrut(120));
    ButtonBox.add(yesButton);
    ButtonBox.add(Box.createHorizontalStrut(20));
    ButtonBox.add(cancelButton);

    /*标签和输入框Box*/
    LabelFieldBox=Box.createHorizontalBox();
    LabelFieldBox.add(Box.createHorizontalStrut(20));
    LabelFieldBox.add(LabelBox);
    LabelFieldBox.add(Box.createHorizontalStrut(10));
    LabelFieldBox.add(FieldBox);
    LabelFieldBox.add(Box.createHorizontalStrut(20));

    /*baseBox*/
    BaseBox=Box.createVerticalBox();
    BaseBox.add(Box.createVerticalStrut(12));
    BaseBox.add(LabelFieldBox);
    BaseBox.add(Box.createVerticalStrut(8));
    BaseBox.add(ButtonBox);
    BaseBox.add(Box.createVerticalStrut(10)); //add_User_Window.removeAll();
    add_User_Window.add(BaseBox);
    add_User_Window.setBounds(120,125,308,220);
    add_User_Window.setBackground(Color.blue);
    add_User_Window.setResizable(false);
    add_User_Window.show(); //复合框事件监听器
    userRightSet.addActionListener(new ActionListener()
    {
    @Override
    public void actionPerformed(ActionEvent e)
    {
    // TODO Auto-generated method stub
    JComboBox cb=(JComboBox)e.getSource();
    userRightSet_select=(String)cb.getSelectedItem();
    }
    });

    /*确定 按钮事件*/
    yesButton.addActionListener(new ActionListener()
    {
    @Override
    public void actionPerformed(ActionEvent e)
    {
    // TODO Auto-generated method stub
    //获得输入框中的值
    passwordText=new String(password.getText().toString());
    confirmPasswordText=new String(confirmPassword.getText().toString());
    userRightSet_selectText=userRightSet_select;
    user=new String(userName.getText().toString());
    System.out.println(user+"daozhelima");
    System.out.println(passwordText);
    if(!user.equals("")&&!passwordText.equals("")&&passwordText.equals(confirmPasswordText))
    {
    insertData(user,passwordText,userRightSet_selectText);//插入数据
    System.out.println("到这里");
    System.out.println(user);
    Vector v2=new Vector();//接受数据并显示在列表中
    v2.add(user);
    System.out.println(user+"lalalalalalalal");
    v2.add(userRightSet_selectText);
    tableModel.addRow(v2);
    JOptionPane.showMessageDialog(addUser_Notice,
    "添加用户成功",
    "温馨提示",
    JOptionPane.INFORMATION_MESSAGE);
    }
    else
    {
    JOptionPane.showMessageDialog(addUser_Notice,
    "添加用户失败",
    "温馨提示",
    JOptionPane.INFORMATION_MESSAGE);
    }

    //userName.setText(null);
    }
    });

    cancelButton.addActionListener(new ActionListener()
    {
    @Override
    public void actionPerformed(ActionEvent arg0)
    {
    // TODO Auto-generated method stub
    add_User_Window.setVisible(false);
    }
    });
    }
    }
      

  6.   

    JButton yesButton=new JButton("确认");
    JButton cancelButton=new JButton("取消");
    Box LabelBox,FieldBox,LabelFieldBox,ButtonBox,BaseBox;//使用BoxLayout
    这些插件 在每次点击增加用户的时候都是新的重新new的
    但是
    JFrame add_User_Window
    JLabel userNameLabel =new JLabel("新用户:");
    //userNameLabel.setFont(new Font("微软雅黑",Font.BOLD,18));
    JLabel passwordLabel =new JLabel("密码:");
    JLabel confirmPasswordLabel=new JLabel("确认密码:");
    JLabel userRightSetLabel=new JLabel("权限:");
    至始至终就一个
    虽然你每次点击增加新用户时都重新new 了button
    但是每次new的和上次关闭的的button是一样的 我输出来看过了 坐标之类的都一样
    javax.swing.JButton[,120,0,60x26,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@3ee284,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResourc[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=确认,defaultCapable=true]
    add_User_Window.dispose();并没有销毁窗口只是影藏了
    这样就很明显了 你点击了那个按钮 其实点击了两个按钮先前的那一个一起点了 而且你每次触发新增用户都会给按钮重新添加yesButton.addActionListener(new ActionListener() 动作监听,当然是执行两次按钮事件 依次类推三次 四次
      

  7.   

    public void actionPerformed(ActionEvent e) {    if(add_User_Window==null){
         add_User_Window=new JFrame();
        }else{
         add_User_Window.setVisible(true);
         return;
        }
    /*标签Box*/
    LabelBox=Box.createVerticalBox();
    LabelBox.add(userNameLabel);
    LabelBox.add(Box.createVerticalStrut(12));
    LabelBox.add(passwordLabel);
    LabelBox.add(Box.createVerticalStrut(12));
    LabelBox.add(confirmPasswordLabel);
    LabelBox.add(Box.createVerticalStrut(12));
    LabelBox.add(userRightSetLabel);在这个动作函数里添加红色这一段就可以了