如果你的输出结果是:
点击鼠标
鼠标单击了两次
那么可能是以下原因:鼠标每次单击都会产生 mouseClicked 事件,但第一次产生时,clickCount 不是2而是1,此时会输出 else 语句。
详细情况请看:
if the first mouse button is pressed, events are sent in the following order:
id modifiers button
MOUSE_PRESSED: BUTTON1_MASK BUTTON1
MOUSE_RELEASED: BUTTON1_MASK BUTTON1
MOUSE_CLICKED: BUTTON1_MASK BUTTON1
When multiple mouse buttons are pressed, each press, release, and click results in a separate event.
For example, if the user presses button 1 followed by button 2, and then releases them in the same order, the following sequence of events is generated:
id modifiers button
MOUSE_PRESSED: BUTTON1_MASK BUTTON1
MOUSE_PRESSED: BUTTON2_MASK BUTTON2
MOUSE_RELEASED: BUTTON1_MASK BUTTON1
MOUSE_CLICKED: BUTTON1_MASK BUTTON1
MOUSE_RELEASED: BUTTON2_MASK BUTTON2
MOUSE_CLICKED: BUTTON2_MASK BUTTON2
If button 2 is released first, the MOUSE_RELEASED/MOUSE_CLICKED pair for BUTTON2_MASK arrives first, followed by the pair for BUTTON1_MASK.参考来自:
http://download.oracle.com/javase/1.4.2/docs/api/java/awt/event/MouseEvent.html#getClickCount()