2016-05-16 134 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

我做到了,但它似乎仍然没有工作。我的意思是它编译,但它似乎仍然没有执行。这可能与我的其他班级有关,但有其他想法。 –