gpt4 book ai didi

java - 需要有关 getChildrenUnmodifying 的解释

转载 作者:行者123 更新时间:2023-12-01 18:11:25 24 4
gpt4 key购买 nike

我在 JavaFX 中修改 root 时遇到问题。

类加载 FXML 并创建场景,但我无法使用 getChildren()函数,只有 getChildrenUnmodifiable() .

login.fxml中的第一个容器是 StackPane ,也许StackPane不支持它,比如某些控件?

我可能误解了 JavaFX 的工作原理。

我创建了一个测试应用程序来查看项目的其他类是否导致了问题:

@Override
public void start(Stage primaryStage) throws Exception {
Parent rootScene = FXMLLoader.load(getClass().getResource("login.fxml"));
Scene scene = new Scene(rootScene, 900, 500);
//rootScene.getChildrenUnmodifiable()
}

谁能解释一下为什么吗?

最佳答案

正如另一个答案中所指出的,Parent.getChildren() 是一个protected 方法。但是,它在 StackPane 中被重写(实际上在 Pane 中,因此对于所有 Pane 子类都是如此),并且可见性扩大到 公开

因此,由于 FXML 的根实际上是 StackPane,因此您所要做的就是更改 root 的编译时类型:

@Override
public void start(Stage primaryStage) throws Exception {
StackPane rootScene = FXMLLoader.load(getClass().getResource("login.fxml"));
rootScene.getChildren().add(...);
Scene scene = new Scene(rootScene, 900, 500);
}

关于java - 需要有关 getChildrenUnmodifying 的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32727592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com