JavaFX - 如何从AnchorPane中删除特定节点 [英] JavaFX - How to delete a specific Node from an AnchorPane
问题描述
我正在使用SceneBuilder 8.0.0和JavaFX 8.
我有一个 Button btn
和一个标签lbl
附加到 AnchorPane ap
。
申请开始时, btn
和 lbl
附加到 ap
。
如何删除其中一个节点?(i只知道clear()方法从
ap
中删除所有节点。
谢谢。
在JavaFX中,可以简单地从< a href =https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Parent.html>父级(例如AnchorPane )使用 .getChildren()
跟随 .remove(对象o)
参考
所以如果你有这些节点的直接引用,你可以使用以下代码从 AnchorPane
中删除 Button
:
ap.getChildren()。remove(btn);
查询
如果你,对某些人来说原因,没有引用按钮btn
你可以使用lookup(String selector)可以像这样找到并删除它:
<$ 。p $ p>
ap.getChildren()除去(ap.lookup( '按钮'));
FXML
或者最后,因为你你正在使用SceneBuilder(以及fxml),你也可以确保你已经连接了一个Controller 并为你的Button分配一个id来获取一个引用并将其删除如下:
// ...在你班级的某个地方
@FXML
private Button myButtonId;
// ...方法中的某个地方
ap.getChildren()。remove(myButtonId);
I'm using SceneBuilder 8.0.0 and JavaFX 8.
I have a Button btn
and a Label lbl
attached to an AnchorPane ap
.
When the application starts, btn
and lbl
are attached to ap
.
How can i delete one of these nodes ? (i only know clear() method which deletes all the nodes from
ap
). thanks.
In JavaFX, nodes can simply be removed from a Parent (e.g. an AnchorPane) using .getChildren()
following by .remove(Object o)
Reference
So if you have a direct reference to these Nodes, you could use the following code to remove the Button
from the AnchorPane
:
ap.getChildren().remove(btn);
Lookup
If you, for some reason, don't have a reference to the Button btn
you can use lookup(String selector) to find and remove it like so:
ap.getChildren().remove(ap.lookup('.button'));
FXML
Or finally, since you are using SceneBuilder (and thus fxml) you could also make sure you hava a Controller connected and assign an id to your Button to get ahold of a reference and remove it like so:
// ... somewhere in your class
@FXML
private Button myButtonId;
// ... somewhere in a method
ap.getChildren().remove(myButtonId);
这篇关于JavaFX - 如何从AnchorPane中删除特定节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!