如何获取场景中节点的实际位置.绝对的位置,不管任何容器/转换.
例如,我想要翻译某个节点a,以便它暂时与另一个节点b重叠.所以我希望将其translateX属性设置为b.globalX-a.globalX.
文件说:
Defines the X coordinate of the
translation that is added to the
transformed coordinates of this Node
for the purpose of layout. Containers
or Groups performing layout will set
this variable relative to
layoutBounds.minX in order to position
the node at the desired layout
location.For example,if child should have a
final location of finalX:
child.layoutX = finalX - child.layoutBounds.minX;
也就是说,任何节点的最终坐标应该是
finalX = node.layoutX + node.layoutBounds.minX
但是运行以下代码:
var rect; Stage { title: "Application title" width: 250 height:250 scene: Scene { content: [ Stack{content:[rect = Rectangle { width:10 height:10}] layoutX:10} ] } } println("finalX = {rect.layoutX+rect.layoutBounds.minX}");
给我finalX = 0.0,而不是finalX = 10.0,因为文档看起来状态.
有没有一个明确的方法来获得JavaFX中绝对最终的定位坐标?
解决方法
对于边界:
bounds = rect.localToScene(rect.getBoundsInLocal());
为JavaFx 1和2工作.