Skip to content
Advertisement

Problem with Inner Class. Illegal Start of Expression error

I am practicing using inner classes but am having difficulty with a homework question: It is as follows:

Create a Swing component class BetterButtons that extends JPanel and has three Jbutton instances labelled “One”, “Two”, and “Three”. In the constructor of BetterButtons, write a local class ButtonListener that implements ActionListener. This local class has a field String name and a constructor that takes a String parameter that it assigns to the field name. The method void actionPerformed outputs on the console notification that button labeled name has been pressed. In the constructor of BetterButtons, create three instances of ButtonListener, one for each button listening to its actions.

I am almost finished, however, I get an illegal start of expression error at the line:

JavaScript

Here is my code:

JavaScript

Also, how can I reference the proper value to be assigned to the string variable name?

Thank you in advance

Advertisement

Answer

I think your definition of buttonListener should be:

JavaScript

And then pass a name to each instantiation of buttonlistener, e.g.:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement