How to record the number of mouse clicks
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.
精彩评论