- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前正在开始使用 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
节点发生了什么?最佳答案
如 Node
的 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()
方法返回如果你能够在两个地方有相同的实例?没什么,这不可能。一个实例只能存在于一个地方。
如果要重复使用该按钮,则必须复制该按钮。
关于java - 我如何在 javafx 8 中多次使用一个控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29537491/
我是一名优秀的程序员,十分优秀!