2011-01-26 17 views
1

このメソッドはJFrameオブジェクトの内部にあります。そのJFrameオブジェクトをその内部クラスのメソッドの引数としてどのように渡すことができますか? 私のコードは次のとおりです。 コメントは私が行うに興味を持って何かを説明:あなたのメソッドが静的でない場合内部クラスからクラスオブジェクトにアクセス

public void runTime(){   
     ActionListener action = new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       count++;     
       text.setText(new Integer(count).toString()); 
       while (count==2012){ 
        //I want to pass the frame that holds this rather than null, how it is possible? 
        JOptionPane.showMessageDialog(null, "HelloEnd", "End of World", JOptionPane.INFORMATION_MESSAGE, new ImageIcon("explode.jpg")); 
        break; 
       } 
      } 
     }; 
     tr = new Timer(1000,action); 
     tr.start(); 
    } 
+2

可能重複[あなたはJavaで匿名内部クラスから外側のクラスへの参照を取得する方法は?](http://stackoverflow.com/questions/31201/how-do-youを試してみてください-get-a-enclasses-an-an-an-inner-class-from-an-an-inner-class-i) – Riduidel

答えて

3

うまくいけば、この質問はanswered beforeではありませんでした。とにかく、

JOptionPane.showMessageDialog(JFrame.this, "HelloEnd", "End of World", JOptionPane.INFORMATION_MESSAGE, new ImageIcon("explode.jpg")); 
+0

ありがとうございます。できます。 – zdcobran

0

私の知る限り、あなたはthissuperキーワードを使用することができます。ここにはtutorialがあります。例:

JOptionPane.showMessageDialog(this, "HelloEnd", "End of World", JOptionPane.INFORMATION_MESSAGE, new ImageIcon("explode.jpg")); 
+0

このキーワードを使用すると、これはActionListenerクラスのコード内のアクションオブジェクトを表します。私は、親クラスのコンポーネントをouterclassから作成したいと思っています。 – zdcobran

1

OuterClassName.thisを使用できます。

+0

実際、私は以前この問題に陥ってしまった。問題のコンテキストは同じで、内部クラスからクラスを囲むための参照にアクセスしなければなりませんでした。答えてくれてありがとう。それは素晴らしいです、私の問題を解決しました。 – zdcobran

関連する問題