我如何在 javafx 8 中多次使用一个控件?
How can i use a control more than one time in javafx 8?
我目前正在开始使用 javafx 8,并在一个简单的解决方案中提出了以下问题:
我有不同的控件(Button
),应该会出现
- 在主要内容(中心的一个
Pane
)
- 在页脚中(
Pane
的底部)
Button one = new Button("1");
Button two = new Button("2");
Button three = new Button("3");
VBox vbox = new VBox();
vbox.getChildren().addAll(one, two, three);
HBox hbox = new HBox();
hbox.getChildren().addAll(two, three); //To clarify my problem i leave one node in vbox
现在似乎发生了最后一个 .addAll()
、 删除了 另一个框中的引用。
BorderPane root = new BorderPane();
root.setCenter(vbox);
root.setBottom(hbox);
输出:
我尝试(为了测试)简单地重复使用一个按钮,但是:
root.setCenter(one);
root.setBottom(one);
结果
java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.RuntimeException: Exception in Application start method
...
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = BorderPane@1784a61
这让我想到了以下问题:
- 除了创建新的按钮实例之外,是否有解决该问题的方法?
-
HBox
和 VBox
节点发生了什么?
- 为什么 控件不能重复使用?
在 JavaFX 中,节点只能在场景图中使用一次。这是有道理的,因为一个节点,例如,包含一个位置。如果你要使用它两次,你将需要两个位置。
如 Node
class 的 JavaDocs 中所述:
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.
因此,您不能做您想做的事。一个按钮只能显示一次,你不能在两个地方有相同的按钮。为了使这一点更清楚 - 例如应该是什么getParent()
方法 return 如果您能够在两个地方拥有相同的实例?没什么,这不可能。一个实例只能存在于一个地方。
如果要重复使用按钮,您必须复制它。
您遇到的错误
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = BorderPane@1784a6
与场景显示 vbox 中的 "one" button
和 hbox 中的 "two"
和 "three"
相关。您只声明了 3 个按钮,场景只能显示 3 个按钮。根据我的评论,您需要声明按钮 4 和 5 并将它们添加到 hbox 中,您可能会看到所有 5 个按钮。
我不知道为什么会这样,但它与控件的初始化有关。结果也可能是它向 vbox 添加了 3 个按钮,向 hbox 添加了 none。但是因为 hbox 在 vbox 之后初始化,这就是为什么它将按钮 2 和 3 放在 vbox 中并将它们丢弃在 hbox 中。(或者实际上抛出异常)
我目前正在开始使用 javafx 8,并在一个简单的解决方案中提出了以下问题:
我有不同的控件(Button
),应该会出现
- 在主要内容(中心的一个
Pane
) - 在页脚中(
Pane
的底部)
Button one = new Button("1");
Button two = new Button("2");
Button three = new Button("3");
VBox vbox = new VBox();
vbox.getChildren().addAll(one, two, three);
HBox hbox = new HBox();
hbox.getChildren().addAll(two, three); //To clarify my problem i leave one node in vbox
现在似乎发生了最后一个 .addAll()
、 删除了 另一个框中的引用。
BorderPane root = new BorderPane();
root.setCenter(vbox);
root.setBottom(hbox);
输出:
我尝试(为了测试)简单地重复使用一个按钮,但是:
root.setCenter(one);
root.setBottom(one);
结果
java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.RuntimeException: Exception in Application start method
...
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = BorderPane@1784a61
这让我想到了以下问题:
- 除了创建新的按钮实例之外,是否有解决该问题的方法?
-
HBox
和VBox
节点发生了什么? - 为什么 控件不能重复使用?
在 JavaFX 中,节点只能在场景图中使用一次。这是有道理的,因为一个节点,例如,包含一个位置。如果你要使用它两次,你将需要两个位置。
如 Node
class 的 JavaDocs 中所述:
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 childrenObservableList
of aParent
, or as the clip of aNode
.If a program adds a child node to a
Parent
(includingGroup
,Region
, etc.) and that node is already a child of a differentParent
or the root of aScene
, the node is automatically (and silently) removed from its former parent.
因此,您不能做您想做的事。一个按钮只能显示一次,你不能在两个地方有相同的按钮。为了使这一点更清楚 - 例如应该是什么getParent()
方法 return 如果您能够在两个地方拥有相同的实例?没什么,这不可能。一个实例只能存在于一个地方。
如果要重复使用按钮,您必须复制它。
您遇到的错误
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = BorderPane@1784a6
与场景显示 vbox 中的 "one" button
和 hbox 中的 "two"
和 "three"
相关。您只声明了 3 个按钮,场景只能显示 3 个按钮。根据我的评论,您需要声明按钮 4 和 5 并将它们添加到 hbox 中,您可能会看到所有 5 个按钮。
我不知道为什么会这样,但它与控件的初始化有关。结果也可能是它向 vbox 添加了 3 个按钮,向 hbox 添加了 none。但是因为 hbox 在 vbox 之后初始化,这就是为什么它将按钮 2 和 3 放在 vbox 中并将它们丢弃在 hbox 中。(或者实际上抛出异常)