我是添加按钮Button监控时出错的Button.addActionListener(this)

解决方案 »

  1.   

    Button b = new Button();
     b.addActionListener(this);
    this类必须实现 Event事件,才能让this监听
      

  2.   

    Button b = new Button();
     b.addActionListener(this);
    其实我忘记说了,我已经加了Button的对象button1了,不过还是出现那个提示提示构造函数中泄漏 this
    import java.awt.*;
    import java.awt.Font.*;
    import java.awt.event.*;
    import java.util.Date;
    import javax.swing.*;
    import java.sql.*;
    import  com.dbmanage.*;
    public class Dl_Zc extends JFrame  implements Runnable , ActionListener {
              public String YH_Name;
            public  String Sex1;
             public String  JG;
              public  String DZ;
               public  String phone;
                   public  String MM;
               public  String E_Mail;
        Thread thread2=new Thread(this);
        Label lable10=new Label("用户注册界面");
          Label lable1=new Label("用户昵称");
            Label lable11=new Label("性别");
    Choice sex=new Choice();
            Label lable2=new Label("籍贯");
            Label lable3=new Label("地址");
            Label lable4=new Label("联系方式");
            Label lable5=new Label("新密码");
            Label lable6=new Label("确认密码");
            Label lable7=new Label("E_Mail");
            JButton Button1=new JButton("注册");
                  JButton Button2=new JButton("取消");
                  TextField text1=new     TextField (5);
                   TextField text2=new     TextField (10);
                   TextField text3=new     TextField (20);
                   TextField text4=new     TextField (15);
                 TextField text5=new     TextField (10);
                    TextField text6=new     TextField (10);
                    TextField text7=new     TextField (15);
           Dl_Zc(){
          
            Box baseBox ,baseBox0 ,boxV0,boxV1,boxV2,boxV3,boxV5,boxV4;
        boxV0=Box.createVerticalBox();
          boxV0.add(lable10);
            lable10.setFont(new java.awt.Font("SansSerif",1,20));
                   lable10.setForeground(Color.red);
           boxV1=Box.createVerticalBox();
          boxV1.add(lable1);
           boxV1.add(Box.createVerticalStrut(38));
          boxV1.add(lable11);
          boxV1.add(Box.createVerticalStrut(38));
          boxV1.add(lable2);
          boxV1.add(Box.createVerticalStrut(38));
           boxV1.add(lable3);
          boxV1.add(Box.createVerticalStrut(38));
          boxV1.add(lable4);
            boxV1.add(Box.createVerticalStrut(38));
          boxV1.add(lable5);
           boxV1.add(Box.createVerticalStrut(38));
          boxV1.add(lable6);
            boxV1.add(Box.createVerticalStrut(38));
          boxV1.add(lable7);
          //boxV1.add(Box.createVerticalStrut(138));
         // boxV1.add(button1);
          boxV2=Box.createVerticalBox();      boxV2.add(text1);
                 boxV2.add(Box.createVerticalStrut(38));
          boxV2.add(sex);
               boxV2.add(Box.createVerticalStrut(38));
          boxV2.add(text2);
          boxV2.add(Box.createVerticalStrut(38));
          boxV2.add(text3);
          boxV2.add(Box.createVerticalStrut(38));
          boxV2.add(text4);
           boxV2.add(Box.createVerticalStrut(38));
          boxV2.add(text5);
                 boxV2.add(Box.createVerticalStrut(38));
          boxV2.add(text6);
                 boxV2.add(Box.createVerticalStrut(38));
          boxV2.add(text7);         boxV5=Box.createHorizontalBox();
          boxV5.add(Button1);
          boxV5.add(Box.createHorizontalStrut(60));
         boxV5.add(Button2);      baseBox=Box.createHorizontalBox();
          baseBox.add(boxV1);
          baseBox.add(Box.createHorizontalStrut(2));
          baseBox.add(boxV2);       baseBox0=Box.createVerticalBox();
          baseBox0.add(boxV0);
          baseBox0.add(Box.createVerticalStrut(38));
           baseBox0.add( baseBox);
           baseBox0.add(Box.createVerticalStrut(38));
           baseBox0.add(  boxV5);
          setLayout(new FlowLayout());      add(baseBox0);
          sex.add("男");
                sex.add("女");
                 // Button1.addActionListener(this);
                     //      Button2.addActionListener(this);
          
           }
            public void run()
        {}
    public void actionPerformed(ActionEvent e){
           // if(e.getSource()==Button1){
                YH_Name=text1.getText().trim();
                Sex1=sex.getSelectedItem().trim();
                JG=text2.getText().trim();
                DZ=text3.getText().trim();
               phone=text4.getText().trim();
                MM=text5.getText().trim();            E_Mail=text7.getText().trim();
            // String  insertString="INSERT INTO Accout VALUES("+"'"+IDint2+"'"+","+"'"+Khmc1+"'"+","+"'"+spmc1+"'"+","
              //  +jg1+","+"'"+MH1+"'"+","
              //  +"'"+ ks1+"'"+","+"'"+ys1+"'"+","+"'"+ml1+"'"+","+"'"+sl1+"'"+","+zjg1+","+"'"+IsBill1+"'"+","+"'"+nowTime+"'"+")";    try{
        JDBCFile conn2= new JDBCFile();
    //    int flag=conn2.insert(insertString);
       
        }
              catch(Exception ee){
               System.out.println(ee);
           }
     JOptionPane.showMessageDialog(null, "    提交成功!","系统信息", JOptionPane.PLAIN_MESSAGE);
            }
    }
    //}
      

  3.   

    这个提示错误比较明显:import java.awt.*;
    import java.awt.Font.*;
    import java.awt.event.*;
    import java.util.Date;
    import javax.swing.*;
    import java.sql.*;
    import  com.dbmanage.*;
    public class Dl_Zc extends JFrame  implements Runnable , ActionListener {
              public String YH_Name;
            public  String Sex1;
             public String  JG;
              public  String DZ;
               public  String phone;
                   public  String MM;
               public  String E_Mail;
        Thread thread2=new Thread(this);
        Label lable10=new Label("用户注册界面");
          Label lable1=new Label("用户昵称");
            Label lable11=new Label("性别");
    Choice sex=new Choice();
            Label lable2=new Label("籍贯");
            Label lable3=new Label("地址");
            Label lable4=new Label("联系方式");
            Label lable5=new Label("新密码");
            Label lable6=new Label("确认密码");
            Label lable7=new Label("E_Mail");
            JButton Button1=new JButton("注册");
                  JButton Button2=new JButton("取消");
                  TextField text1=new     TextField (5);
                   TextField text2=new     TextField (10);
                   TextField text3=new     TextField (20);
                   TextField text4=new     TextField (15);
                 TextField text5=new     TextField (10);
                    TextField text6=new     TextField (10);
                    TextField text7=new     TextField (15);
           Dl_Zc(){
          
            Box baseBox ,baseBox0 ,boxV0,boxV1,boxV2,boxV3,boxV5,boxV4;
        boxV0=Box.createVerticalBox();
          boxV0.add(lable10);
            lable10.setFont(new java.awt.Font("SansSerif",1,20));
                   lable10.setForeground(Color.red);
           boxV1=Box.createVerticalBox();
          boxV1.add(lable1);
           boxV1.add(Box.createVerticalStrut(38));
          boxV1.add(lable11);
          boxV1.add(Box.createVerticalStrut(38));
          boxV1.add(lable2);
          boxV1.add(Box.createVerticalStrut(38));
           boxV1.add(lable3);
          boxV1.add(Box.createVerticalStrut(38));
          boxV1.add(lable4);
            boxV1.add(Box.createVerticalStrut(38));
          boxV1.add(lable5);
           boxV1.add(Box.createVerticalStrut(38));
          boxV1.add(lable6);
            boxV1.add(Box.createVerticalStrut(38));
          boxV1.add(lable7);
          //boxV1.add(Box.createVerticalStrut(138));
         // boxV1.add(button1);
          boxV2=Box.createVerticalBox();      boxV2.add(text1);
                 boxV2.add(Box.createVerticalStrut(38));
          boxV2.add(sex);
               boxV2.add(Box.createVerticalStrut(38));
          boxV2.add(text2);
          boxV2.add(Box.createVerticalStrut(38));
          boxV2.add(text3);
          boxV2.add(Box.createVerticalStrut(38));
          boxV2.add(text4);
           boxV2.add(Box.createVerticalStrut(38));
          boxV2.add(text5);
                 boxV2.add(Box.createVerticalStrut(38));
          boxV2.add(text6);
                 boxV2.add(Box.createVerticalStrut(38));
          boxV2.add(text7);         boxV5=Box.createHorizontalBox();
          boxV5.add(Button1);
          boxV5.add(Box.createHorizontalStrut(60));
         boxV5.add(Button2);      baseBox=Box.createHorizontalBox();
          baseBox.add(boxV1);
          baseBox.add(Box.createHorizontalStrut(2));
          baseBox.add(boxV2);       baseBox0=Box.createVerticalBox();
          baseBox0.add(boxV0);
          baseBox0.add(Box.createVerticalStrut(38));
           baseBox0.add( baseBox);
           baseBox0.add(Box.createVerticalStrut(38));
           baseBox0.add(  boxV5);
          setLayout(new FlowLayout());      add(baseBox0);
          sex.add("男");
                sex.add("女");
                 Button1.addActionListener(this);
              Button2.addActionListener(this);
          
           }
            public void run()
        {}
    public void actionPerformed(ActionEvent e){
           if(e.getSource()==Button1){
                YH_Name=text1.getText().trim();
                Sex1=sex.getSelectedItem().trim();
                JG=text2.getText().trim();
                DZ=text3.getText().trim();
               phone=text4.getText().trim();
                MM=text5.getText().trim();            E_Mail=text7.getText().trim();
            // String  insertString="INSERT INTO Accout VALUES("+"'"+IDint2+"'"+","+"'"+Khmc1+"'"+","+"'"+spmc1+"'"+","
              //  +jg1+","+"'"+MH1+"'"+","
              //  +"'"+ ks1+"'"+","+"'"+ys1+"'"+","+"'"+ml1+"'"+","+"'"+sl1+"'"+","+zjg1+","+"'"+IsBill1+"'"+","+"'"+nowTime+"'"+")";    try{
        JDBCFile conn2= new JDBCFile();
    //    int flag=conn2.insert(insertString);
       
        }
              catch(Exception ee){
               System.out.println(ee);
           }
     JOptionPane.showMessageDialog(null, "    提交成功!","系统信息", JOptionPane.PLAIN_MESSAGE);
            }
    }
    }
      

  4.   

    if(e.getSource()==Button1){
    if(e.getSource()==Button1){上面代码 这个括号是中文的改过来,你看你代码  我发的已经改过来了
    其他的 我电脑没有找到 JDBCFile  类,其他代码没有问题
      

  5.   

    谢谢大家!呵呵,我也是发上去才发现是中文括号的错,不过那个this泄露是啥意思,我是初学的,不是很懂。
      

  6.   

    典型的事件监听机制,Button就是被监听的对象
      

  7.   

    同问, 每次 addActionListener(this) 都会提示警告, 想从根本上知道为什么, 不怕解释复杂, 只要合理就好.
      

  8.   

    我今天也遇到了这个问题,感觉是个警告,就是说在构造函数执行的过程中,对象还没有被创建出来,或者说还没有被初始化好,这时把this拿来用,有点不妥当的意思,但仔细想一下,执行构造函数时,对象地址已经分配好,引用this应该是没有问题的,或者说,可能绝大多数情况下没有问题,不知道会不会在某种情况下有问题,提示的警告是“泄漏this”,也许在安全上会有问题吧,期待有更深入的解答,如果有可能的话,先把对象创建好,再用对象的引用去set或调用某方法,这样就不用在构造方法里引用this了,只是考虑一下并发的问题,不要多线程同时调用。另外如果把这个引用this的语句单拿到一个方法里去,在构造方法里去调用这个方法,不会有警告了,只是这是欺骗编译器的一种行为吧,没有从根本上解决在执行构造方法时引用this的问题。