我创建了一个图像查看器,可以在鼠标按下时放大,在释放鼠标时缩小。按住鼠标时,您可以拖动图像:
public void dragOnMouseMove( MouseEvent event ) {
if( zoomedIn ) {
if( mouseStartX != null && mouseStartY != null ) {
double x = this.getTranslateX() - (mouseStartX - event.getX());
double y = this.getTranslateY() - (mouseStartY - event.getY());
LOG.debug( "moving picture to: " + x + "," + y );
this.setTranslateX( x );
this.setTranslateY( y );
}
mouseStartX = event.getX();
mouseStartY = event.getY();
}
}
这绑定(bind)到拖动事件。 问题是:当鼠标到达屏幕边界时,拖动停止,因为坐标没有更多变化。 有没有一种方法可以独立于光标位置来检测鼠标移动?
最佳答案
如果我很了解您正在寻找 MouseInfo.getPointerInfo().getLocation()可能会有帮助。它返回与当前鼠标位置对应的 Point 对象。
关于JavaFx检测鼠标移动(到达屏幕边界后),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37446541/