2014-04-26 43 views
0

有沒有辦法在點擊鼠標按鈕和釋放這個按鈕之間以秒或毫秒爲單位的時間。在MouseClicked事件上捕獲秒數

我用這個功能,但不適合我

public void mouseClicked (MouseEvent me) { 
    long time=me.getWhen(); 
    // other stuff 
} 

一些幫助?

+0

可能重複http://stackoverflow.com/questions/1360818/javascript-how-to-measure-the-milliseconds-between-mousedown-and-mouseup – MutantMahesh

回答

1

它不適合你,因爲getWhen()返回事件發生時的毫秒數,而不是事件持續的時間。 此代碼的工作對我來說:

long mousePressed; 
long mouseReleased; 
long mousePressTime; 

@Override 
public void mousePressed(java.awt.event.MouseEvent e) { 
    mousePressed = e.getWhen(); 
} 

@Override 
public void mouseReleased(java.awt.event.MouseEvent e) { 
    mouseReleased = e.getWhen(); 

    mousePressTime = mouseReleased - mousePressed; 

    System.out.println("PRESS TIME "+mousePressTime); 
} 
+0

+1,非常感謝你,我已經嘗試了幾種類型的實現,這很好。 – OiRc