在 JavaFX 中获取节点的真实位置 [英] Get real position of a node in JavaFX

查看:23
本文介绍了在 JavaFX 中获取节点的真实位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 JavaFX 中获取节点绝对位置的最佳方法是什么?

What is the best way to get the absolute position of a node in JavaFX?

想象一下,我们在窗格(Hbox、Stackpane 或任何其他窗格)中有一个节点,并且它本身可能有一个父节点.

Imagine we have a node in a Pane (Hbox, Stackpane, or any other pane) and that may have a parent itself.

我想获取该节点的绝对位置并在另一个窗格中使用它.

I want to get the absolute 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()code>、getWidth()getHeight() 方法.

In either case the resulting Bounds object has getMinX(), getMinY(), getMaxX(), getMaxY(), getWidth() and getHeight() methods.

这篇关于在 JavaFX 中获取节点的真实位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆