简体   繁体   English

如何知道鼠标是否在Java中的jpanel中向左或向右拖动

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

I have mouseMotionListener in my jpanel code. 我的jpanel代码中有mouseMotionListener。

But how can I know if the mouse dragged to left or right inside the jpanel? 但是如何知道鼠标是在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); 在事件回调中:存储mouse-(x-)位置,在下一个回调中计算与上一个位置的差(并再次存储该位置); 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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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