开发者

How to record the number of mouse clicks

开发者 https://www.devze.com 2023-01-09 14:44 出处:网络
How to record the number of mouse clicks in Java开发者_开发问答?Something along the lines of this should do it, Adding the MouseListener on any component you want to listen for clicks on

How to record the number of mouse clicks in Java开发者_开发问答?


Something along the lines of this should do it, Adding the MouseListener on any component you want to listen for clicks on

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)  { }

}


Note that the Java mouse events already have a click counter.


Generally speaking, you would attach a mouse click event handler, and whenever your handler gets called, you would increment an integer counter.

0

精彩评论

暂无评论...
验证码 换一张
取 消