如何获取鼠标/任意坐标下的控件列表(或第一个)?我知道WPF有VisualTreeHelper.HitTest,它有一个回调函数,可用于过滤掉某一点的所有控件.JavaFX有类似的东西吗?(或者不同,我只关心给定点的第一个元素)我已经看到很多信息来获取节点的坐标,但没有关于如何通过坐标获取节点的信息.
2021 年 12 月更新
答案中提到的 impl_pickNode 函数已被删除并替换为备用拾取功能。
javafx.scene.Scene 源代码中演示了类似的功能,其中有一个函数仅用于测试 JavaFX 系统:
/**
 * Note: The only user of this method is in unit test: PickAndContainTest.
 */
Node test_pick(double x, double y) {
    inMousePick = true;
    PickResult result = mouseHandler.pickNode(new PickRay(x, y, 1,
            Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY));
    inMousePick = false;
    if (result != null) {
        return result.getIntersectedNode();
    }
    return null;
}
此功能仍然是 JavaFX 系统中的私有功能,它不会作为受支持的公共 API 公开。所以 JavaFX 内部知道如何做到这一点,如果您愿意调查内部源代码并使用内部 API,打破模块化并妥协向后兼容性,那么您可能会得到结果。
你可以使用node.impl_pickNode(x,y).
该方法的文档(从源impl_pickNode复制)。
/** 
 * Finds a top-most child node that contains the given coordinates.
 *
 * Returns the picked node, null if no such node was found.
 *
 * @deprecated This is an internal API that is not intended for use 
 *             and will be removed in the next version. 
 **/
public final Node impl_pickNode(double parentX, double parentY)
请仔细注意评论中的弃用警告,并自行承担使用风险。
更新
JavaFX 问题跟踪器中有一个现有的功能请求:FX 应该提供 Parent.pick() 例程。此功能请求是针对公共拣选 API 的,该 API 将来不会被弃用。所请求的功能被描述为:“例程可以返回单个节点或按 z 坐标排序的鼠标下方的所有节点的列表”。该功能预计要等到“Van Ness”版本(即最初的 JDK8 版本之后的版本)才会实现(即该功能最早要到 2013 年圣诞节才能使用)。
| 归档时间: | 
 | 
| 查看次数: | 2962 次 | 
| 最近记录: |