我创建了以下小程序-->
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Adapting extends Applet
{
public void init()
{
addMouseListener(new MyMouseAdapter(this));
addMouseMotionListener(new MyMouseAdapter(this));
}
}//end of class
class MyMouseAdapter extends MouseAdapter
{
Adapting a;
public MyMouseAdapter(Adapting a)
{
this.a=a;
}
public void mouseClicked(MouseEvent me)
{
a.setBackground(Color.red);
}
public void mouseMoved(MouseEvent me)
{
a.showStatus("Moving Mouse at "+me.getX()+","+me.getY());
}
}
/*<applet code=Adapting height=500 width=600></applet>*/
对于与 MouseEvent 对应的两个侦听器,即MouseListener
和MouseMotionListener
,我使用适配器类MouseAdapter
。如果,MouseAdapter
班级也正在完成我的工作以收听鼠标运动事件,那么班级有什么用MouseMotionAdapter
?