MouseMotionListener在鼠標移動到組件上時觸發事件,而mouseListener只在按下按鈕時觸發?Java中mouseListener和mouseMotionListener的區別?
所以,如果我只有一個mousePressed事件,那麼我不需要mouseMotionListener?只有當我有鼠標輸入或鼠標退出?
MouseMotionListener在鼠標移動到組件上時觸發事件,而mouseListener只在按下按鈕時觸發?Java中mouseListener和mouseMotionListener的區別?
所以,如果我只有一個mousePressed事件,那麼我不需要mouseMotionListener?只有當我有鼠標輸入或鼠標退出?
是的,你是對的。 mouseMotionListener
用於執行操作時,鼠標在移動「熱點」
很好的例子可以發現here
當mousePressed
事件處理,你只需要mousePressed
事件,除非你想添加更多的事件來執行,而鼠標懸停。
他們傾聽不同的事件:
mouseClicked(MouseEvent event) // Called just after the user clicks the listened-to component.
mouseEntered(MouseEvent event) // Called just after the cursor enters the bounds of the listened-to component.
mouseExited(MouseEvent event) // Called just after the cursor exits the bounds of the listened-to component.
mousePressed(MouseEvent event) // Called just after the user presses a mouse button while the cursor is over the listened-to component.
mouseReleased(MouseEvent event) // Called just after the user releases a mouse button after a mouse press over the listened-to component
mouseDragged(MouseEvent event) // Called in response to the user moving the mouse while holding a mouse button down. This event is fired by the component that fired the most recent mouse-pressed event, even if the cursor is no longer over that component.
mouseMoved(MouseEvent event) // Called in response to the user moving the mouse with no mouse buttons pressed. This event is fired by the component that's currently under the cursor.
根據你是什麼事件後添加監聽器。