haha
quite a few.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
haha
quite a few.
Last edited by newtolearningjava; April 26th, 2014 at 05:51 PM.
Only add the listener once. It will be called for each time it is added.
If you don't understand my answer, don't ignore it, ask a question.
please can you show me the line of code you use to only add a listener once?
public class game extends JFrame implements ActionListener{
isit like this?
It is not possible to control the logic with a single line of code.
For example: If the addListener() method is in a loop it will be added many times.
Or if it is in another method that is called many times, it will be called each time.
If you don't understand my answer, don't ignore it, ask a question.
how do i change these
Last edited by newtolearningjava; April 26th, 2014 at 05:52 PM.
What good is that? There needs to be code that can be tested.
If you don't understand my answer, don't ignore it, ask a question.
if i pits my
Last edited by newtolearningjava; April 26th, 2014 at 05:52 PM.
Find why the actionPerformed() method is called more than once?
Add a println() next to the line where the addActionListener() method is called to see how many times it is called. If it is called more than once, then you need to move the call to addActionListener() to where it is only called one time.
If you don't understand my answer, don't ignore it, ask a question.
Looks like the same listener is added 4 times to 4 different buttons. That wouldn't be a problem if only one button is pressed.
I'm done for tonight. Back tomorrow.
If you don't understand my answer, don't ignore it, ask a question.
I have been playing around with the game. I did what you said and added the system.out.println as you can see in the code below:
Last edited by newtolearningjava; April 26th, 2014 at 05:53 PM.
To see what events are causing the actionPerformed() method to be called, add a println statement to print out the ActionEvent object that is passed to that method. A lot of info will be printed and somewhere in that printout you can see what component sent the event.
If you don't understand my answer, don't ignore it, ask a question.
Did you look at what was printed out in the actionPerformed() method to see what component sent the ActionEvent?
I've lost my version of the source for this problem. I can't compile and execute it for testing.
If you don't understand my answer, don't ignore it, ask a question.