文档 – 在JavaFX中获取节点的全局坐标

如何获取场景中节点的实际位置.绝对的位置,不管任何容器/转换.

例如,我想要翻译某个节点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工作.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...