JavaFX FXML模式不支持窗格内的上下文菜单的理由是什么? [英] What is the justification for JavaFX FXML schema not supporting context menu inside the panes?

查看:336
本文介绍了JavaFX FXML模式不支持窗格内的上下文菜单的理由是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以将上下文菜单添加到滚动窗格,但不能添加到其他类型的窗格。为什么?

It is possible to add a context menu to a scroll pane, but not to other types of panes. Why?

推荐答案

FXML如何工作

FXML通过使用反射(或使用专门的构建器类)对Java API进行内省工作。有关FXML工作的更多信息,请参阅简介到FXML 文档。

FXML works by introspecting on the Java API using reflection (or by using specialized builder classes). More information on FXML works can be found in the Introduction to FXML documentation.

为什么无法使用FXML标记在JavaFX中的窗格上定义ContextMenus

Control有一个 contextMenu属性 ScrollPane 是一个控件。其他窗格类型(如 StackPane )不是控件。由于这些其他窗格类型中没有相应的属性可以设置为包含对contextMenu的引用,因此无法使用FXML在这些窗格类型上定义contextMenu。

Control has a contextMenu property. A ScrollPane is a Control. Other pane types such as StackPane are not controls. As there is no corresponding property in these other pane types which could be set to contain a reference to a contextMenu, you can't define a contextMenu on these pane types using FXML.

出于类似的原因,您无法在窗格上定义工具提示。

For similar reasons, you can't define a Tooltip on a Pane either.

如何为FXML控制器中的窗格定义ContextMenu

您仍然可以通过代码使用contextMenu show API,例如将以下代码放在FXML控制器中。

You can still set a context menu on panes (and any other arbitrary nodes which are not controls) via code, using the contextMenu show API, for example by placing the following code in your FXML controller.

@FXML StackPane stack;

// . . .

public void initialize() {
    final ContextMenu contextMenu = new ContextMenu(new MenuItem("xyzzy"));
    stack.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent mouseEvent) {
            contextMenu.show(
                    stack,
                    mouseEvent.getScreenX(), 
                    mouseEvent.getScreenY()
            );
        }
    });
}

为什么不添加ContextMenu属性

Node可以有一个contextMenu属性,它允许通过FXML Markup在Panes上定义ContextMenus。

Node could have a contextMenu property, which would allow ContextMenus to be defined on Panes via FXML Markup.

之所以如此节点没有contextMenu属性,但Control确实没有因为ContextMenu本身就是一个控件。向节点添加ContextMenu属性意味着JavaFX实现的核心场景图模型代码将依赖于控件模块(这将依赖于场景图模块),因此是循环依赖。这将阻止运行非常轻的Java运行时系统,其中包括核心JavaFX场景图和渲染引擎,但不包括控件(不是今天任何人都有这样的系统)。

The reason why Node does not have a contextMenu property but Control does is because ContextMenu is itself a Control. Adding a ContextMenu property to node would mean that the core scene graph model code for the JavaFX implementation would have a dependency on the controls module (which would have a dependency on the scene graph module), hence a circular dependency. This would prevent the shipping of a very light Java runtime system which included the core JavaFX scene graph and rendering engine but did not include controls (not that anybody ships such a system today).

如何归档功能请求

如果您认为应该更改系统以允许使用SceneBuilder在任意窗格上定义上下文菜单,那么您可以提交针对JavaFX问题跟踪器的功能请求(如果您这样做,请包含一个返回的链接功能请求中的这个问题)。

If you think the system should be changed to allow definition of context menus on arbitrary panes using SceneBuilder, then you can file a feature request against the JavaFX issue tracker (if you do so, include a link back to this question in the feature request).

这篇关于JavaFX FXML模式不支持窗格内的上下文菜单的理由是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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