Reputation: 29
So I have a nested class in my public static void main. It seems to compile and be correct but the function it is supposed to perform doesn't seem to execute. Here is the method.
public static void main(String[]args)
{
LevelOne l = new LevelOne();
//Level Two not made yet just a place holder to show constructor with a different type
LevelTwo l2 = new LevelTwo();
//I make l2 first because the front frame is the last one created
Main m = new Main(l2);
Main m2 = new Main(l);
//To switch levels i am going to load them all in advance and then when the beat the level it will close the frame
class ChoiceListener implements ActionListener
{
Timer tm = new Timer(5, this);
//timer is used for the actionlistener
public void actionPerformed(ActionEvent e)
{
if(l.checkWin())
{
m2.setVisible(false);
m2.dispose();
}
}
}
}
And here is the variable it is supposed to access form level l in the other class.
public void setWin()
{
this.checkWin = true;
}
public boolean checkWin()
{
return this.checkWin;
}
checkWin is a private instance field for the other class. For some reason when checkWin is set to true it still wont execute. Any help would be appreciated!
Upvotes: 0
Views: 298
Reputation: 879
Add below line in main method
Timer tm = new Timer(5, new ChoiceListener());
tm.start();
and remove
Timer tm = new Timer(5, this);
from ChoiceListener
. Also move ChoiceListener
out of main method in the class so that it can be instantiated using class instance or make it static so that it can be directly instantiated.
Upvotes: 1