Hi I've been reading tutorials and even books and so far from what I've read I'm doing the right code yet I'm getting an error.can someone please help me with this,this is very discouraging.
only thing you have to really read is the end of the code
here is my code,everything works until I add the 'if(event.getSource()==button)' part of the code,what could be possibly wrong it looks like the correct syntax from what I've read,if someone can help me out with this really discouraging situation I would be more than thankfull,thanks alot
The actionPerformed() method is being passed an event which is referenced by 'e', your if statement is using 'event' which is a totally different variable.
Joined: Aug 07, 2007
I've just noticed that also your 'button' is variable is declared as local to the mess() constructor so isn't even visible in the actionPerformed method. In fact this code won't even compile, are you sure it's the code you are running?
that might be syntax which works, and which you find in many books and the Java Tutorials, but I think it is non‑OO design. Create a class which implements the Action Listener interface. You can have it as a private inner class, a public top‑level class or an anonymous class. The link I gave you suggests a class which handles such an event.
You will doubtless know there are scores of different kinds of event; if you move a Swing display across the screen, you get many events occurring per second, but you ignore most of them by not listening for them. I suggest you try the colour listener class, in this sort of code:-Never mind about the rest of the code. Just a frame with a panel and that button on. No need to confuse things with lots of other components or code; you will have enough difficulty remembering which colour it turns things
This looks like a GUI‑related problem, so I shall move the discussion to our GUIs forum. I shall also try adding code tags to your post; they should make it look a lot better.
Joined: Nov 27, 2013
Thanks Tony,Thanks campbell,
but still a little stuck,how can I actually make the event.getSource work,thanks
Joined: Oct 13, 2005
Adam Chalkley wrote: . . . how can I actually make the event.getSource work,thanks
You add a different Listener to each control.Or you find somebody colour‑prejudiced who won't mislead you with the wrong colours.