2016-05-16 137 views
0

所以我在我的公共靜態無效主要嵌套類。它似乎編譯和正確,但它應該執行的功能似乎並沒有執行。這是方法。動作監聽器和內部類java

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時,它仍然不會執行。任何幫助,將不勝感激!

+0

Main也是類的名稱這就是爲什麼建設者說主要抱歉,如果有任何混淆因此。 –

+0

你在哪裏創建你的監聽器類的實例並使用它?你只是現在宣佈它。 – Sanjeev

+0

這些Java Swing/AWT控制權?因爲如果他們是,我想我已經知道發生了什麼:) –

回答

1

添加下面線在主方法

Timer tm = new Timer(5, new ChoiceListener()); 
tm.start(); 

和從ChoiceListener除去

Timer tm = new Timer(5, this); 

。還要將ChoiceListener移出課程中的主要方法,以便可以使用類實例對其進行實例化,或將其設置爲靜態,以便可以直接實例化。

+0

我做到了,但它似乎仍然沒有工作。我的意思是它編譯,但它似乎仍然沒有執行。這可能與我的其他班級有關,但有其他想法。 –