简体   繁体   English

从嵌套的 Java ActionListener 内部调用它

[英]Calling this from inside a nested Java ActionListener

Suppose I have this:假设我有这个:

class external {
    JFrame myFrame;
    ...

    class internal implements ActionListener {
        public void actionPerformed(ActionEvent e) {
             ...
             myFrame.setContentPane(this.createContentPane());
        }
    }
    ...
}

createContentPane returns a Container. createContentPane返回一个容器。 Now, if I was doing this code outside of the ActionListener it would work, because I'd have access to this.现在,如果我在ActionListener之外执行此代码,它会起作用,因为我可以访问它。 But, inside it, I don't.但是,在里面,我没有。 I have access to myFrame , which is what is going to be updated with the contents of the method, but that isn't enough to do what I want, unless I can get a this out of it.我可以访问myFrame ,这将使用方法的内容进行更新,但这还不足以做我想做的事,除非我能从中得到这个。

I also need information from other instance variables to use createContentPane() , so I'm not sure I can make it static .我还需要来自其他实例变量的信息才能使用createContentPane() ,所以我不确定我是否可以使它成为static

You can either:您可以:

myFrame.setContentPane(createContentPane());

or或者

myFrame.setContentPane(external.this.createContentPane());

By the way, in Java classes first letter is usually uppercase.顺便说一句,在 Java 类中,第一个字母通常是大写的。 Your code will still compile and run if you don't name it like that, but by following coding conventions you'll be able to read others code, and much more important other will be able to read your code.如果您不这样命名,您的代码仍然可以编译和运行,但是通过遵循编码约定,您将能够阅读其他代码,更重要的是其他人将能够阅读您的代码。

So this would be a better style:所以这将是一个更好的风格:

class External {
    JFrame myFrame;
    ...

        class Internal implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                ...
                myFrame.setContentPane(createContentPane());
               //Or myFrame.setContentPane(External.this.createContentPane());
            }
        }
    ...
 }

Java Code conventions Java 代码约定

external.this will give you access to the instance of the enclosing class, if that's what you want... external.this将使您能够访问封闭 class 的实例,如果这是您想要的...

Not sure exactly what you're getting at, but an inner class has access to all of the members of its enclosing class.不确定您到底在做什么,但内部 class 可以访问其封闭 class 的所有成员。 To access the "this" pointer of the enclosing class (eg, to pass to other methods) use:要访问封闭 class 的“this”指针(例如,传递给其他方法),请使用:

someMethod(External.this);

In your example, you're actually complicating it by using "this".在您的示例中,您实际上是通过使用“this”使其复杂化。 Here are two options that will work:以下是两个可行的选项:

myFrame.setContentPane(createContentPane());

or:或者:

myFrame.setContentPane(External.this.createContentPane());

Note that you're already accessing myFrame in the same manner.请注意,您已经在以相同的方式访问 myFrame。

First you have to extend JFrame in your outer class like this首先,您必须像这样在外部 class 中扩展 JFrame

class External extends JFrame {
.....
.....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM