简体   繁体   中英

how to know if mouse dragged left or right inside jpanel in java

I have mouseMotionListener in my jpanel code.

But how can I know if the mouse dragged to left or right inside the jpanel?

In the event callback: store the mouse-(x-)position, in the next callback calculate the difference to the previous position (and store the position again); depending on the sign(um) you can determine whether it was a left or right drag.

Use

if (currentX > previousX) {
    // Right
} else {
    // Left
}
previousX = currentX;

in your listener.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM