b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ea){
System.exit(0);
}
});
I am learning Java and saw the above code.
I can't understand why the addActionlisetner
method needs Actionlistener
for the argument.
Isn't it simpler to just use System.exit(0)
?
You have the the API Java as reference to find the answer of your question.
For example, the concrete class JButton inherited the method
addActionListener(ActionListener l)
from the class javax.swing.AbstractButton.When you do :
You're creating an instance of an anonymous subclass of ActionListener.
ActionListener is an interface made for receiving actions events.
The API says: