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...
Type: Posts; User: Norm
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...
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...
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.
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...
What good is that? There needs to be code that can be tested.
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...
Only add the listener once. It will be called for each time it is added.
Interesting? I've never seen that. Usually its 2 like the code posted earlier.
Where are the statements that create those 4 instances?
Is there a loop that creates the 4 new instances of the...
The simple answer is to change the code so that it only opens one.
How many are openned?
Are all the new GUIs the same or are they all different?
How many places does the code create a GUI? Why...
Good, now look at how the code in post#45 works and copy that technique to the other code.
If there error messages, copy the full text and paste it here.
That code isn't any good for testing. It needs to be complete.
Did you miss post#40 about removing "frame."?
Post the new version of the code.
I made the changes I've described and it works for me.
How many lines of code did you remove? You were only supposed to remove ONE line:
JFrame frame = new JFrame();
That required that all usages of the variable: frame be changed to use the JFrame...
This one:
JFrame frame = new JFrame();
and use the one that Game extends.
There should only be one JFrame created. Get rid of one of them.
The code creates two JFrames:
new game();
JFrame frame = new JFrame();
and closes the game frame. The JFrame referred to by the frame variable is not closed.
Can you make a small, complete program that compiles, executes and shows the problem?
Add a call to setVisible(false);
What happened when you compiled and executed the code?
Are you saying you do not know how to call a method?
You code must be full of calls to methods.
For example these statements are calls to methods:
dispose();
...
That's a lot closer. I used a D not a d in the name. What you have will work, but it doesn't follow the naming conventions.
What was the name of the method I suggested be added?
What is the name of the method you added?
Are they the same?
That's your answer.
Take it one step at a time.
Another way:
Add a method named: callDispose() to the Game class. Have that method call dispose().
From the listener method in the inner class, call the callDispose() method.
Where is the class with the dispose() method? You need a reference to the class's object to be able to call its dispose() method. The posted code does not show a class that has a dispose() method.
You need a reference to the object to be able to call its methods.
The this keyword refers to the object that it is executing in. The example shown:
this.dispose()
doesn't do anything...