gpt4 book ai didi

java - 查找对象时javafx中的空指针异常

转载 作者:太空宇宙 更新时间:2023-11-04 00:46:18 24 4
gpt4 key购买 nike

我需要在我的 FXML Controller 中引用三个省略号:

<Ellipse fx:id="selectorontop" id="selectorontop" fill="WHITE" layoutX="121.0" layoutY="101.0" radiusX="14.0" radiusY="27.0" stroke="WHITE" strokeType="INSIDE" style="-fx-opacity: 70%;" visible="false" />
<Ellipse fx:id="selectoronmiddle" id="selectoronmiddle" fill="WHITE" layoutX="121.0" layoutY="168.0" radiusX="14.0" radiusY="27.0" stroke="WHITE" strokeType="INSIDE" style="-fx-opacity: 70%;" visible="false" />
<Ellipse fx:id="selectoronbottom" id="selectoronbottom" fill="WHITE" layoutX="120.0" layoutY="466.0" radiusX="14.0" radiusY="27.0" stroke="WHITE" strokeType="INSIDE" style="-fx-opacity: 70%;" visible="false" />

场景创建后传递给 Controller ​​:

public class QuickCopy extends Application {

@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));
AnchorPane root = (AnchorPane)loader.load();

Scene scene = new Scene(root);
stage.setScene(scene);
stage.sizeToScene();
stage.setResizable(false);
stage.show();

MainController controller = (MainController)loader.getController();
controller.sendScene(scene);
System.out.println("new: " +scene.lookup("selectorontop"));
}

Controller 接收到场景,但在主 Java 文件(如上所示)和 Controller 中查找的结果仍然为“null”,我不明白为什么。

提前致谢

最佳答案

您使用的选择器按类型选择,而不是按 ID。它选择 selectorontop 类型的节点,我很确定这种类型不存在。 (至少场景中没有这种类型的节点。)

您需要使用正确的 CSS 选择器。在这种情况下,您需要使用 #selectorontop 按 id 选择:

System.out.println("new: " +scene.lookup("#selectorontop"));

关于java - 查找对象时javafx中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57046846/

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