首页 > 代码库 > Java 鼠标事件监听MouseListener

Java 鼠标事件监听MouseListener

1、鼠标按下 、松开 、进入、移除等时发生动作事件

方法摘要:

MouseClicked

void mouseClicked(MouseEvent e)   鼠标按键在组件上单击(按下并释放)时调用。 

mousePressed

void mousePressed(MouseEvent e)   鼠标按键在组件上按下时调用。  

mouseReleased

void mouseReleased(MouseEvent e)    鼠标按钮在组件上释放时调用。  

mouseEntered

void mouseEntered(MouseEvent e)    鼠标进入到组件上时调用。

mouseExited

void mouseExited(MouseEvent e)    鼠标离开组件时调用。  

 

 


1
public class TestMouseList extends Frame 2 { 3 public static void main(String[] args) 4 { 5 Frame f = new Frame(); 6 f.setBounds(200, 200, 200, 200);//设置窗口的大小 位置 7 f.addMouseListener(new MouseListTest()); 8 f.setVisible(true);//让窗口显示出来 9 }10 }11 class MouseListTest implements MouseListener12 {13 @Override14 public void mouseClicked(MouseEvent e) {15 //System.out.println("点击事件");16 //System.out.println(e.getClickCount());//和鼠标抬起差不多17 }18 @Override19 public void mousePressed(MouseEvent e) {20 //System.out.println("鼠标按下");21 }22 @Override23 public void mouseReleased(MouseEvent e) {24 //System.out.println("鼠标抬起");25 }26 @Override27 public void mouseEntered(MouseEvent e) {28 System.out.println("鼠标进来了");29 }30 @Override31 public void mouseExited(MouseEvent e) {32 System.out.println("鼠标出去了");33 }34 35 }

 

技术分享

Java 鼠标事件监听MouseListener