2015-10-08 33 views
1

X是好的,但奇怪的是Y被幾個像素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()); 

有沒有人有這個問題至今感動?

回答

0

您正在獲取文本字段相對於場景的界限,然後添加窗口的座標。所以你會被窗口的座標和場景的座標之間的差異所抵消(例如標題欄的大小)。

使用

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); 

當然第一個版本看起來更簡潔。