JavaFX - setVisible 不会“隐藏”元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28558165/
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
JavaFX - setVisible doesn't "hide" the element
提问by Gillardo
In JavaFX, if I have a scene with 2 VBox
elements and each VBox
has multiple Label
in it.
If I set the top VBox
to invisible, why does the bottom VBox
not move upthe scene where the top VBox
was ?
在 JavaFX 中,如果我有一个包含 2 个VBox
元素的场景,并且每个元素VBox
都有多个Label
。
如果我将顶部设置VBox
为不可见,为什么底部VBox
不会在顶部所在的场景中向上移动VBox
?
The VBox
is invisiblebut I would expect the other objects to move into its place.
该VBox
是无形的,但我希望其他对象移动到原来的位置。
I am using FXML to load my controls.
我正在使用 FXML 加载我的控件。
回答by breaktop
Since it's invisible, it wont move to the top. You have to remove it with something like:
因为它是隐形的,所以它不会移动到顶部。您必须使用以下内容将其删除:
// remove
vbox.getChildren().remove(...)
Once you've removed the element you want invisible then, the other element should move to the top.
一旦你删除了你想要隐藏的元素,另一个元素应该移动到顶部。
回答by Juce
Instead of hiding the vbox you should removeit from the Children and if you want to show it again addthe vbox again.
您应该将其从 Children 中删除,而不是隐藏 vbox ,如果您想再次显示它,请再次添加vbox。
回答by eckig
Node.setVisible(boolean)
just toggles the visibility state of a Node
.
Node.setVisible(boolean)
只是切换 a 的可见性状态Node
。
To exclude a Node
from its parents layout calculations you additionally have to set its managed state, by calling Node.setManaged(false)
.
要将 aNode
从其父布局计算中排除,您还必须通过调用Node.setManaged(false)
.
If you want the managed state to be updated automatically alongside the visibility, you can use a binding as @jewelsea pointed out: node.managedProperty().bind(node.visibleProperty());
如果您希望托管状态与可见性一起自动更新,您可以使用@jewelsea 指出的绑定: node.managedProperty().bind(node.visibleProperty());
回答by Evaboy
If l want to hide and unhide a node, I resize the node to 0 if l want to hide it. That way, the node will not occupy space since is not visible to the user, so when l want it to be visible, l adjust the size again for it to be visible.
如果我想隐藏和取消隐藏一个节点,如果我想隐藏它,我会将节点的大小调整为 0。这样,节点不会占用空间,因为用户不可见,所以当我希望它可见时,我再次调整大小使其可见。
回答by Nicolescu Ionut Lucian
Try to use setVisible and managedProperty together. Here is an example:
尝试将 setVisible 和 managedProperty 一起使用。下面是一个例子:
myHBox.setVisible(false);
myHBox.managedProperty().bind(myHBox.visibleProperty());