I am sure I have written about anonymous inner classes as ActionListeners before. Of course, other Listeners, as well as Runnables can also be anonymous.
I use the rule of thumb that if you have more than one Listener which do the same thing, or something very similar, use a named class, and pass the differences in the constructor; the classic example given in beginners' exercises is buttons which change their colour; set up this sort of thing:-
I don't like the
format which is found in some books; it might be simple to learn, but if you have more than one button, you can end up with so many if elses and else ifs, that the actionPerformed method looks a right mess. I only use the (this) technique when it specifically refers back to your Component (eg add a MouseMotionListener to a panel which displays the mouse co-ordinates on a text field).
The anonymous inner class is useful when you have
one component with a unique action. An example using an exitButton:- [But if you have an exitButton and an exitMenuItem, don't sue anonymous classes, use an ExitListener class, otherwise you are duplicating things.]
Stage 2: put two new lines in the ()
Stage 3: fill in the gap.
Stage 4: ActionListener is an interface; make it into a class body with {}.
Stage 5: ActionListener must have one method, with the signature as below. It would be more complicated if we used .addMouseListener() because MouseListener has to have five methods
At this point (and not before) you can persuade the compiler to accept it, but nothing will happen until stage 6: Fill in your method body.
And as Joe Ess has told you, putting in a call to another method rather than lots of coding will make it simpler to see what is going on.
I hope that is of some use to you
CR