java - javafx中是否可以让一个节点同时出现在两个不同的阶段?

标签 java javafx

我想知道是否有一种简单的方法可以链接 JavaFX 中处于不同阶段的两个节点的功能。

在一个阶段,我有一个包含 ToggleGroup 中的两个 radioMenuItem 的菜单,它们一起控制应用程序的主题:

BorderPane mainPane = new BorderPane();
MenuBar menuBar = new MenuBar();
menuBar.setStyle("-fx-base: white");
Menu options = new Menu("Options");
MenuItem appearance = new MenuItem("Appearance");

Menu themes = new Menu("Theme");
ToggleGroup toggleGroup = new ToggleGroup();
RadioMenuItem dark = new RadioMenuItem("Dark");
dark.setToggleGroup(toggleGroup);
RadioMenuItem light = new RadioMenuItem("Light");
light.setToggleGroup(toggleGroup);
themes.getItems().addAll(dark, light);

options.getItems().addAll(appearance, themes);
menuBar.getMenus().addAll(options);
mainPane.setTop(menuBar);

此 Pane 属于应用启动时打开的舞台。当一个新窗口打开时,我希望用户也能够从该窗口控制主题。我尝试将相同的项目添加到该阶段的 Pane 中,但随后它们从该阶段的 Pane 中删除。我不想为每个窗口创建新对象,因为它们完全相同,只是在不同的 Pane 和舞台中。

最佳答案

在任何给定时间,一个节点只能附加到一个父节点。

来自Node javadoc :

A node may occur at most once anywhere in the scene graph. Specifically, a node must appear no more than once in all of the following: as the root node of a Scene, the children ObservableList of a Parent, or as the clip of a Node.

If a program adds a child node to a Parent (including Group, Region, etc) and that node is already a child of a different Parent or the root of a Scene, the node is automatically (and silently) removed from its former parent. If a program attempts to modify the scene graph in any other way that violates the above rules, an exception is thrown, the modification attempt is ignored and the scene graph is restored to its previous state.

It is possible to rearrange the structure of the scene graph, for example, to move a subtree from one location in the scene graph to another. In order to do this, one would normally remove the subtree from its old location before inserting it at the new location. However, the subtree will be automatically removed as described above if the application doesn't explicitly remove it.

您希望同时将同一个节点连接到两个不同的场景,这在设计上是不可能的。

关于java - javafx中是否可以让一个节点同时出现在两个不同的阶段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72766077/

相关文章:

events - 事件发生时出现 IllegalArgumentException,javafx

java - 隐藏和显示其他类的 javafx 阶段并保留所有信息

java - 在单独线程上处理请求后写入非阻塞 NIO UDP (DatagramChannel) 套接字

java - 在 Android 中每 10 分钟调用一个方法

java - 如何在 Java String 中用一个双引号替换两个双引号?

java - 如何在 JavaFX 中展开和折叠 TitledPane

java - Spring Data Rest 按关键字搜索

java - 使用 iText java 创建的带有水印图像的 PDF 文件

java - setOnMouseExited/Entered 的奇怪行为

javafx - Stage.setIconified() 和 Stage.isIconified() 运行不正常