我正在开始使用它自己的 Controller 的第二个场景。我想从另一个类访问该 Controller 中的方法。如何获取新场景 Controller 的句柄?
public void startNewScene() throws IOException{
Stage stage = new Stage();
Partent root;
root = FXMLLoader.load(getClass().getResource("fxmlfile.fxml");
Scene scene = new Scene(root);
Stage.setScene(scene);
stage.show();
}
最佳答案
创建一个 FXMLLoader
实例(而不是使用static load(...)
方法),并从中获取 Controller :
public void startNewScene() throws IOException{
Stage stage = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("fxmlfile.fxml"));
Parent root = loader.load();
MyController controller = loader.getController();
Scene scene = new Scene(root);
Stage.setScene(scene);
stage.show();
}
显然将 MyController
替换为 fxmlfile.fxml
Controller 的实际类名。
关于java - 如何获取新阶段 Controller JavaFX 的句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40344351/