0
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Appwindow extends Frame{
    String keymsg="This is a test";
    String mousemsg="";
    int mouseX=0, mouseY=0;
    public Appwindow()
    {
            addKeyListener(new MyKeyAdapter(this));
        addMouseListener(new MyMouseAdapter(this));
        addWindowListener(new MyWindowAdapter());
    }

    public void paint(Graphics g)
    {
        g.drawString(keymsg,10,40);
        g.drawString(mousemsg,mouseX,mouseY);
    }

    public static void main(String args[])
    {
        Appwindow ap = new Appwindow();
        ap.setSize(new Dimension(300,300));
        ap.setTitle("Application");
        ap.setVisible(true);
    }
}

class MyKeyAdapter extends KeyAdapter{
    Appwindow a;
    public MyKeyAdapter(Appwindow a)
    {
       this.a=a;
        }
    public void keyTyped(KeyEvent e)
    {
    a.keymsg += e.getKeyChar();
    a.repaint(); 
   }
}

class MyMouseAdapter extends MouseAdapter{
Appwindow a;
public MyMouseAdapter(Appwindow a)
{
    this.a=a;
}
public void mousePressed(MouseEvent e)
{
    a.mouseX=e.getX();
    a.mouseY=e.getY();
    a.mousemsg="Mouse down at" + a.mouseX+ "," + a.mouseY;
    a.repaint();
}
}

 class MyWindowAdapter extends WindowAdapter{
  public void windowClosing(WindowEvent e)
 {
    System.exit(0);
}
}

这是制作基于框架的窗口程序的简单代码。我只想问在注册事件监听器时使用它有什么意义......就像在addMouseListener(new MyMouseAdapter(this));

请向我解释这行代码中实际发生了什么。

4

5 回答 5

0

我知道这个问题很老,但我想知道同样的问题并遇到了这个问题。提供的答案确实帮助了我,但没有为我提供完整的图片。

我从以下我发表评论的程序中理解了这个概念,以便其他有相同问题的人可以更容易地理解这个概念。

 public class MousePressedDemo extends Applet {

   public void init(){

  //question was why we are passing "this" in line below
  //at this point just understand like any use of "this" keyword 
  // it points to current
  //object of class MousePressedDemo or current applet window
  //we are passing it to the constuctor of MyMouseAdapter class

  addMouseListener(new MyMouseAdapter(this); // why we pass this in this line 

  }

 } // closing class MousePressedDemo 

 class MyMouseAdapter extends MouseAdapter{

 // line below creates ref var of class MousePressedDemo 
 //to which we can assign an object of type class MousePressedDemo

 MousePressedDemo mousePressedDemo;  

 //below constructor taking object of class MousePressedDemo as parameter.
 public MyMouseAdapter(MousePressedDemo mousePressedDemo){ 

  // the line below is the answer we are looking for. The object of class  
  // or applet window  We pass as "this" is now passed to a ref variable in  
  // this class. now using this reference var we can access the original 
  // applet window. look below to see how it is used to show message  
  // on status bar of original window

   this.mousePressedDemo = mousePressedDemo; 

}// closing constuctor


 public void mousePressed(MouseEvent me){

 // show message on status window of original applet class

mousePressedDemo.showStatus("MOuse Pressed");

 } // close method


} // closing class MyMouseAdapter 
于 2014-07-29T14:03:48.767 回答
0

无论你在哪里使用它总是this 引用当前对象。

MyMouseAdapter(this));

这意味着将电流传递Appwindow 给方法

 public MyMouseAdapter(Appwindow a)

这样你就可以在 MyMouseAdapter方法上做一些任务Appwindow

于 2013-10-12T13:20:22.960 回答
0

您正在告诉动作侦听器在对象(this)(您的窗口)上发生事件时告诉您。您键入鼠标发送的事件。就像您正在订阅电视频道一样。你说,“嘿,听众,当窗口上发生鼠标事件时,告诉我。” 您将在必须为此侦听器实现的接口方法中被告知

于 2013-10-12T13:21:43.097 回答
0

this是仅在实例上下文中可用的引用。也就是说,它引用了您正在操作的当前对象。 AFrame可以注册许多侦听器(KeyListenerMouseListener等)。您的自定义类实现希望对Frame您注册它们有一些参考。实现这一点的一种方法是通过类的构造函数传递对当前Frame(即AppWindow)对象的引用。this

于 2013-10-12T13:22:37.850 回答
0

好的,所以,您正在注册一个侦听器,即实现某个侦听器接口的类,或者在您的情况下,从某个类继承(在您的情况下MouseAdapter)。

因为MyMouseAdapter MouseAdapter 类型,Java 可以指望它有一个方法mousePressed(MouseEvent e)

这是上半场。

现在,碰巧这个鼠标适配器的特定实现想要在按下鼠标时执行某些操作,并且某些操作包括向您的窗口添加一条消息,因此,它需要访问该窗口(AppWindow准确地说) . 您可以在每次调用时将其传递给mousePressed,但您无法定义该接口。相反,您正在做的是在 MouseAdapter 的构造函数中传递 AppWindow 实例。

现在,恰好您是AppWindow 执行此操作的,因此,从您的代码的角度来看,当您调用addMouseListener(new MyMouseAdapter(this));this 时,它指的是当前对象,即 AppWindow

希望有帮助!

于 2013-10-12T13:24:37.780 回答