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));
请向我解释这行代码中实际发生了什么。