2016-05-16 10 views
0

私は私のpublic static void mainに入れ子クラスを持っています。コンパイルして正しいと思われるが、実行するはずの機能は実行されていないようだ。ここにその方法があります。アクションリスナと内部クラス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

メインはクラスの名前でもあります。その理由のためコンストラクタが主な理由を申し訳なく思うのはなぜですか? –

+0

リスナークラスのインスタンスを作成して使用していますか?あなたは今のところそれを宣言しただけです。 – Sanjeev

+0

これらのJava Swing/AWTコントロールはありますか?彼らがいる場合、私はすでに何が起こっているか知っていると思う:) –

答えて

1

は、mainメソッドで線の下に

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

を追加し、ChoiceListenerから

Timer tm = new Timer(5, this); 

を削除します。クラスインスタンスを使用してインスタンス化することもできるし、静的にインスタンス化して直接インスタンス化することもできるように、クラス内のメインメソッドからChoiceListenerを移動します。

+0

私はそれをやったが、まだ動作していないようです。私はそれがコンパイルを意味するが、それはまだ実行しないようだ。それは私の他のクラスに関連するものかもしれませんが、他のアイディアです。 –

関連する問題