Java事件处理:在事件监听器中使用此关键字的重要性

时间:2013-10-12 13:16:48

标签: java awt keylistener windowlistener mouse-listeners

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));

一样

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

5 个答案:

答案 0 :(得分:0)

你总是使用this对当前对象的引用。

MyMouseAdapter(this));

这意味着将当前Appwindow传递给方法

 public MyMouseAdapter(Appwindow a)

这样您就可以在MyMouseAdapter

上的Appwindow方法中执行某项任务

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

由于MyMouseAdapter 类型为MouseAdapter,因此Java可以指望它具有方法mousePressed(MouseEvent e)

那是上半场。

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

现在,碰巧你正在从 AppWindow做这个,所以,从你的代码的角度来看,当你调用mousePressed时,这指的是当前对象,即AppWindow

希望有所帮助!

答案 4 :(得分: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