A listener is called when the user does something to the user interface that causes an event. Altho these events usually come from the user interface, they can have other sources (eg, a Timer).
After a button is created, you will add a listener to it. Eg,
b.addActionListener(listener_object);
When the button is clicked, a call is made to the
actionPerformed() method defined in the class of the
listener object. An ActionEvent object is passed as
a parameter to it actionPerformed().
actionPerformedThere is (unfortunately) no way to use any given method as a listener -- the
listener must be called actionPerformed.
One per class. Because
there can only be one actionPerformed method in
a class, a new class is needed for every separate listener, or you
have to share an actionPerformed method and use
the ugly technique of figuring out who caused the call.
Incidental note. To address this awkward situation C# has delegates, which allow any method to be a listener, provided it has the correct return type and parameters. This doesn't provide any additional functionality, but it is more convenient.
You need to create an instance of
the class which defines the actionPerformed method for the button,
and it is this instance that becomes the button listener.
Here are common ways to write listeners: