What addActionListener does?

后端 未结 5 961
终归单人心
终归单人心 2020-12-31 14:26

I have the following code:

JButton button = new JButton(\"Clear\");
button.addActionListener(this);

As far as I understand I create a butto

5条回答
  •  暖寄归人
    2020-12-31 14:54

    The event generator is told about the object which can handle its events Event Generators have a method; — addActionListener(reference to the object of Handler class) For example,

    JButton b1=new JButton("My Button");
     b1.addActionListener(this); // Please note listener and generator are same class
    

    Since event handler is in same object that contains button, so we have to use this to pass the reference.

提交回复
热议问题