java 克隆 JavaFX 节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14180141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:22:14  来源:igfitidea点击:

Clone JavaFX Node?

javaduplicatesclonejavafxscenebuilder

提问by Shahmeer Navid

I have created a Node(AnchorPane) in the JavaFX scene builder and was wondering how to clone it.

我在 JavaFX 场景构建器中创建了一个Node( AnchorPane) 并且想知道如何克隆它。

I saw Duplicate/Clone Node in JavaFX 2.0but I need to clone the Nodewithout re-loading the fxml.

在 JavaFX 2.0 中看到了重复/克隆节点,但我需要在Node不重新加载 fxml的情况下克隆它。

Is there any way to achieve this in JavaFX 2?

有没有办法在 JavaFX 2 中实现这一点?

回答by Peter Lamberg

You can place the component that needs to be duplicated in a separate .fxmlfile.

您可以将需要复制的组件放在单独的.fxml文件中。

Then you can load the separate file as many times as needed adding the nodes to the appropriate root in the main scene.

然后您可以根据需要多次加载单独的文件,将节点添加到主场景中的适当根。

Additionally you can edit an <fx:include source="..."/>element to the main .fxmlfile and include the separate .fxmlfile. You can then still work with it in the JavaFX Builder.

此外,您可以将<fx:include source="..."/>元素编辑到主.fxml文件并包含单独的.fxml文件。然后您仍然可以在 JavaFX Builder 中使用它。

回答by jewelsea

There is no such node duplication function in the JavaFX platform, you will need to write your own which introspects on the properties of the node you are interested in cloning and creates a new node with the required properties.

JavaFX 平台中没有这样的节点复制功能,您需要自己编写对您感兴趣的节点的属性进行克隆的自省,并创建一个具有所需属性的新节点。

Using an fxml template for the node definition is probably the easiest way to do this for node's with static initialization properties - but then, as you state in your question, you don't want to use fxml for this, so you'll have to code your cloning logic in Java.

对节点定义使用 fxml 模板可能是对具有静态初始化属性的节点执行此操作的最简单方法 - 但是,正如您在问题中所述,您不想为此使用 fxml,因此您必须用 Java 编写您的克隆逻辑。