从其内部类访问类对象

时间:2011-01-26 09:48:07

标签: java class inheritance

此方法在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();
    } 

3 个答案:

答案 0 :(得分:4)

希望这个问题不是answered before。无论如何,试试

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

答案 1 :(得分:1)

您可以使用OuterClassName.this

答案 2 :(得分:0)

AFAIK,如果您的方法不是静态的,则可以使用thissuper个关键字。这是一些tutorial。例如:

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