import java.awt.*; 
import java.awt.event.*; 

import javax.swing.JFrame; 

//TwoListen类同时实现MouseMotionListener和MouseListener两个接口 
public class TwoListen extends JFrame implements MouseMotionListener, 
MouseListener { 
private TextField tf; 
private String s; 

public TwoListen() { 
super("Two listeners example"); 
getContentPane().add(new Label("Clic and drag the mouse"), 
BorderLayout.NORTH); 
tf = new TextField(30); 
tf.setEditable(false); 
getContentPane().add(tf, BorderLayout.SOUTH); 
// 注册监听程序 
addMouseMotionListener(this);//(************************) 
addMouseListener(this); //(************************) 
setSize(300, 300); 
setVisible(true); //(************************) 
} 

// 实现MouseMotionListener接口中的方法 
public void mouseDragged(MouseEvent e) { 
s = "Mouse dragging:X=" + e.getX() + "Y=" + e.getY(); 
tf.setText(s); 
} 

public void mouseMoved(MouseEvent e) { 
} 

// 实现MouseListener接口中的方法 
public void mouseClicked(MouseEvent e) { 
} 

public void mouseEntered(MouseEvent e) { 
s = "The mouse entered"; 
tf.setText(s); 
} 

public void mouseExited(MouseEvent e) { 
s = "The mouse has left the building"; 
tf.setText(s); 
} 

public void mousePressed(MouseEvent e) { 
} 

public void mouseReleased(MouseEvent e) { 
} 

public static void main(String[] args) { 
TwoListen two = new TwoListen(); 
two.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

} 
