在javaFX中获取节点的实际位置 [英] get real position of a node in javaFX
问题描述
在JavaFX中获取节点绝对位置的最佳方法是什么?
假设我们在一个窗格(Hbox,Stackpane,......或任何其他窗格)中有一个节点,并且可能有父节点。
我想获得该节点的绝对位置并在另一个窗格中使用它?
What is the best way to get absolute position of a node in JavaFX ? Suppose we have a node in a a pane(Hbox, Stackpane , ...or any other pane) and that may have parent itself. I want to get abosolute position of that node and use it in another pane ?
推荐答案
这取决于什么你的意思是绝对的。节点有一个坐标系,它的父坐标系,父节点坐标系等等,最后是 Scene
的坐标系和一个坐标系。屏幕(可能是物理显示设备的集合)。
It depends a little what you mean by "absolute". There is a coordinate system for the node, a coordinate system for its parent, one for its parent, and so on, and eventually a coordinate system for the Scene
and one for the screen (which is potentially a collection of physical display devices).
你可能想要相对于 Scene
的坐标,在这种情况下你可以做
You probably either want the coordinates relative to the Scene
, in which case you could do
Bounds boundsInScene = node.localToScene(node.getBoundsInLocal());
或相对于屏幕的坐标:
Bounds boundsInScreen = node.localToScreen(node.getBoundsInLocal());
在任何一种情况下产生的 Bounds
对象有 getMinX()
, getMinY()
, getMaxX()
, getMaxY( )
, getWidth()
和 getHeight()
方法。
In either case the resulting Bounds
object has getMinX()
, getMinY()
, getMaxX()
, getMaxY()
, getWidth()
and getHeight()
methods.
这篇关于在javaFX中获取节点的实际位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!