如何从处理程序内调用外部方法

发布于 2024-11-30 03:59:01 字数 536 浏览 2 评论 0原文

所以我在公共类中有一个 MouseListener 类,其中包含一些方法。 我已将 mouseListener 附加到公共类中的一个组件。

问题是我无法找到一种简单的方法来调用公共类中的方法,每当我说 this.showRemove(); 时,作用域是来自处理程序类而不是公开课。这是一些示例代码

public class Game {

 public Game() {

  JPanel pnl = new JPanel();
  pnl.addMouseListener(new GameMouseListener());

 }

 public void showRemove(){

  //Code Here

 }


 class GameMouseListener implements MouseListener {

  public void mouseClicked(MouseEvent e) {
     this.showRemove(); //Can't Find Symbol Here     

  }

 }  

}

So I have A MouseListener class inside a public class with a few methods.
I have attached the mouseListener to a component in the public class.

The problem is I can't figure out a simple way to call the methods in the public class, whenever I say for example this.showRemove(); the scope is from within the handler class and not the public class. Here is some example code

public class Game {

 public Game() {

  JPanel pnl = new JPanel();
  pnl.addMouseListener(new GameMouseListener());

 }

 public void showRemove(){

  //Code Here

 }


 class GameMouseListener implements MouseListener {

  public void mouseClicked(MouseEvent e) {
     this.showRemove(); //Can't Find Symbol Here     

  }

 }  

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怀里藏娇 2024-12-07 03:59:01

当您在内部类中使用 this 时,您指的是内部类的实例,而不是宿主类。

由于您的内部类不是静态内部类,因此您可以通过使用主机类的类名来访问对主机类的引用,如下所示:

HostClass {
    doSomething() {}

    class Inner {
        void blah() {
            HostClass.this.doSomething();
        }
    }
}

除非您调用的方法也在内部类中,否则您可以避免显式命名,可以简单地使用方法名称:

void blah() {
    doSomething();
}

When you use this in an inner class, you are referring to the instance of the inner class, not the host class.

Since your inner class is not a static inner class, you can access the reference to the host class by using it's class name like so:

HostClass {
    doSomething() {}

    class Inner {
        void blah() {
            HostClass.this.doSomething();
        }
    }
}

Unless the method you're calling is also in the inner class, you can avoid the explicit naming and can simply use the method name:

void blah() {
    doSomething();
}
难得心□动 2024-12-07 03:59:01

由于 showRemoveGame 的非静态方法,因此您需要该类的一个实例来调用该方法。

您可以创建一个如下所示的匿名内部类:

pnl.addMouseListener(new MouseAdapter() {
  public void MouseClicked (MouseEvent e) {
    showRemove();
  }
});

此侦听器将与正在运行的 Game 实例关联,因此可以访问其非静态方法。

Since showRemove is a non-static method of Game you need an instance of that class on which to call the method.

You could instead create an anonymous inner class like this:

pnl.addMouseListener(new MouseAdapter() {
  public void MouseClicked (MouseEvent e) {
    showRemove();
  }
});

This listener will be associated with the running instance of Game and will therefore have access to its non-static methods.

晨光如昨 2024-12-07 03:59:01

您需要对 Game 对象的引用才能调用该方法。当您说 this.showRemoved() 时,“this”引用的是 MouseListener 对象而不是 Game 对象。

一种可能是让 Game 类扩展 MouseListener,并将 mouseClicked() 方法放在 Game 类中。

You need a reference to a Game object to be able to call the method. When you say this.showRemoved() "this" is referencing the MouseListener object instead of the Game object.

One possibility is to have the Game class extend MouseListener, and put the mouseClicked() method inside the Game class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文