Strange problems with Java JPanel and addActionListener

Started by
1 comment, last by aattss3 11 years, 10 months ago
As you see, I did this project in Java involving JPanel. However, I had three seperate classes for the different menus, and I wanted to merge them. However, now I get the following error message:

The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (JPanel)

I have attached the txt files of the source code. Can anyone help me?

Edit:Long story short, I fixed some things and re-uploaded it here because it's now complaining about static modifiers.
Advertisement
So what the error is saying is that you cannot make a GUI element an action listener, in this case JPanel.

At a quick glance, the rest of your code seems ok, but there are multiple lines where you are trying to add your panel as an action listener, from what I can see is the following lines:

[source lang="java"] attack.addActionListener(panel);
exit.addActionListener(panel);

lightning.addActionListener(panel);[/source]

And there are a few other simular problems in your UpgradeMenu method. You need to change it to something like (element.addActionListener(this);).

You will get another error which I spotted though:

[source lang="java"]panel.add(panel);[/source]
This is just rendundant, you cant add something to itself tongue.png I think you probably wanted to do the following:

[source lang="java"]this.add(panel);[/source]
As this is pointing to your Jframe which contains your various panels.
Thanks for the info, although it seems to be happy with addActionListener now. I fixed some other errors if I remember correctly, but unfortunately, it now seems to be complaining about how some things aren't static. However, originally, everything was static but it didn't work so I changed it. I have reuploaded the new files to the main post.

This topic is closed to new replies.

Advertisement