一千萬個為什麽

搜索

如何記錄鼠標點擊次數

如何記錄Java中的鼠標點擊次數?

最佳答案

應該做的就是這樣做,在要監聽點擊的任何組件上添加MouseListener

public class MyFrame extends JFrame implements MouseListener{

   /** Number of times the mouse was clicked */
   private int clicks = 0;

   public MyFrame() 
   {
      this.addMouseListener(this);
   }

   public void mouseClicked(MouseEvent e) 
   { 
       //Increment click count
       clicks++;
   }

   public void mouseEntered(MouseEvent e) {}
   public void mouseExited(MouseEvent e) {}
   public void mousePressed(MouseEvent e){}    
   public void mouseReleased(MouseEvent e)  { }

}

轉載註明原文: 如何記錄鼠標點擊次數