em> work:If you move the lookup code to after you show the stage, it might work:@Overridepublic void start(Stage stage) throws Exception{ FXMLLoader loader = new FXMLLoader(getClass().getResource("MainWindow.fxml")); Parent root = loader.load(); Scene scene = new Scene(root, 600, 575); stage.setScene(scene); stage.setTitle("Test"); stage.setResizable(false); stage.show(); System.out.println(scene.lookup("#vBox"));}查询一般来说不是很强大。如上所述,您应该只是访问控制器中的FXML元素。只是为了给你另一个选项,你可以从 FXMLLoader 获得命名空间,这是一个 Map< String,Object> 包含FXML的所有元素,其中包含 fx:id s(以及其他内容)。 Lookups in general are not very robust. As indicated above, you should really just access elements of the FXML in the controller. Just to give you one other option, you can get the namespace from the FXMLLoader, which is a Map<String, Object> containing all the elements of the FXML which have fx:ids (among other things). 所以你可以这样做: FXMLLoader loader = new FXMLLoader(getClass().getResource("MainWindow.fxml")); Parent root = loader.load(); Map<String, Object> namespace = loader.getNamespace(); System.out.println(namespace.get("vBox"));除此之外,请注意 Controller controller = new Controller(); controller.setScene(scene);什么都不做。创建新控制器与获取 FXMLLoader 为您创建的控制器不同。 (无论如何,你总是可以在控制器中执行 vBox.getScene()来获取场景。)doesn't do anything. Creating a new controller is not the same as getting the controller that was created for you by the FXMLLoader. (It's redundant anyway, you can always do vBox.getScene() in the controller to get the scene.) 这篇关于如果我搜索我的VBox,scene.lookup()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 21:09