问题
I want to bind the position of a pane that pops up to the position of the Button calling it, and I tried doing so by callling button.localToScene(button.getLayoutX(),button.getLayoutY()
. However the returned Point2D variable has the wrong X coordinate in it. I measured it in Gimp and its supposed to be at 320 but it returns me 586.
Same story with localToScreen()
btw, I dont even have 586 pixels of screen estate until the button already shows up, so its impossible for it to be at that coordinate.
回答1:
You may want to get the button's node position relevant to the element that contains the button such as a pane or the scene, if you provide code it would help. But something like this where you get the location of the pane that contains your button, then get ur button position and work out the difference. But i suggest trying this:
Bounds boundsInScene = button.localToScene(button.getBoundsInLocal());
Or this relative to the screen:
Bounds boundsInScreen = button.localToScreen(button.getBoundsInLocal());
Then use the methods : getMinX(), getMinY(), getMaxX(), getMaxY(), getWidth() or getHeight() to get the values of your actual button.
Hope this helps.
来源:https://stackoverflow.com/questions/38612350/getting-position-in-scene-of-a-node-with-localtoscenex-y-returns-wrong-value