楼主,挑战一下
import java.awt.*;
import java.awt.event.*;
//TwoListen类同时实现MouseMotionListener和MouseListener两个接口
publicclass TwoListen implements MouseMotionListener,MouseListener
{
private Frame f;
private TextField tf;
publicstaticvoid main(String[] args)
{
TwoListen two=new TwoListen();
two.go();
}
publicvoid go()
{
new Frame("Twolisteners example");
f.add(new Label("Clic and drag the mouse"),"North");
tf=new TextField(30);
f.add(tf,"South");
//注册监听程序
f.addMouseMotionListener(this);//(************************)
f.addMouseListener(this); //(************************)
f.setSize(300,300);
f.setVisible(true); //(************************)
}
//实现MouseMotionListener接口中的方法
publicvoid mouseDragged(MouseEvent e)
{
String s="Mouse dragging:X="+e.getX()+"Y="+e.getY();
tf.setText(s);
}
publicvoid mouseMoved(MouseEvent e){}
//实现MouseListener接口中的方法
publicvoid mouseClicked(MouseEvent e){}
publicvoid mouseEntered(MouseEvent e)
{
String s="The mouse entered";
tf.setText(s);
}
publicvoid mouseExited(MouseEvent e)
{
String s="The mouse has left the building";
tf.setText(s);
}
publicvoid mousePressed(MouseEvent e){}
publicvoid mouseReleased(MouseEvent e){}
}