[英]Action Listeners and inner classes java
所以我的公共靜態void main中有一個嵌套類。 它似乎已編譯且正確,但是應該執行的功能似乎未執行。 這是方法。
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();
}
}
}
}
這是應該在另一個類中訪問級別l的變量。
public void setWin()
{
this.checkWin = true;
}
public boolean checkWin()
{
return this.checkWin;
}
checkWin是另一個類的私有實例字段。 由於某些原因,當checkWin設置為true時,它仍然不會執行。 任何幫助,將不勝感激!
在主要方法中添加以下行
Timer tm = new Timer(5, new ChoiceListener());
tm.start();
並刪除
Timer tm = new Timer(5, this);
來自ChoiceListener
。 同樣,將ChoiceListener
從類的main方法中移出,以便可以使用類實例對其進行實例化,或者將其設為靜態,以便可以直接對其進行實例化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.