获取控件的全局屏幕坐标时 Y 坐标不准确

Y coordinate isn't exact when getting a control's global screen coordinates

X没问题,但奇怪的是Y移动了几个像素

Popup resultsList = new Popup();

Bounds textFieldBounds = textField.localToScene(textField.getBoundsInLocal());

ListView lsv = new ListView();
lsv.layoutXProperty().set(0);
lsv.layoutYProperty().set(0);
resultsList.getContent().add(lsv);

Window window = textField.getScene().getWindow();
double x = window.getX();
double y = window.getY();

resultsList.show(textField, 
                     x + textFieldBounds.getMinX(), y + textFieldBounds.getMaxY());

到目前为止有人遇到过这个问题吗?

您正在获取相对于 scene 的文本字段的边界,然后添加 window 的坐标.所以你会被 window 的坐标和场景的坐标(例如标题栏的大小)之间的任何差异所抵消。

使用

Bounds boundsInScreen = textField.localToScreen(textField.getBoundsInLocal());
resultsList.show(textField, boundsInScreen.getMinX(), boundsInScreen.getMaxY());

你也可以

// your existing code

double sceneX = textField.getScene().getX();
double sceneY = textField.getScene().getY();

resultsList.show(textField, x + textFieldBounds.getMinX() + sceneX,
    y + textFieldBounds.getMaxY() + sceneY);

当然第一个版本没有那么冗长。