代码。
import java.awt.*;
import java.awt.event.*;
public class MultListener implements MouseListener,MouseMotionListener,WindowListener
{
private Frame f;
private TextField tf;
public static void main(String [] args)
{
MultListener ml = new MultListener();
ml.go();
}
public void go()
{
Frame f = new Frame("多个监听者");
f.add( new Label("单击和拖动鼠标"),"North");
tf = new TextField(30);
f.add( tf,"South");
f.addMouseListener(this);
f.addMouseMotionListener(this);
f.addWindowListener(this);
f.resize(200,200);
f.show();
}
public void mouseDragged(MouseEvent e)
{
String s = "Mouse dragging : X = "+e.getX()+" Y = "+e.getY();
tf.setText(s);
}
public void mouseMoved(MouseEvent e){}
public void mouseEntered(MouseEvent e)
{
String s = "The mouse Exited";
tf.setText(s);
}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowClosed(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
}