我目前正在将一个小型应用程序从 Swing 重新编码为 JavaFX,因为它似乎是部署我目前拥有的 web 的最简单方法。
我似乎无法做一些非常简单的事情,而且我在文档和其他帖子中迷失了方向:
无论如何,我有调用相关 FXML 文件的主控制器:
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
现在,从 Login.fxml 中,我有一个按钮,我希望该按钮打开另一个 FXML 文件。我可以让按钮成功加载事件,但我尝试了很多东西,但我无法让它工作。我正在尝试这样的事情:
private void handleButtonAction(ActionEvent event){
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("GeneradorBases.fxml"));
// fxmlLoader.setRoot(this);
// fxmlLoader.setController(this);
try {
fxmlLoader.load();
}
catch (IOException e){
throw new RuntimeException(e);
}
}
我尝试按照我在 Stackoverflow 上看到的示例进行操作。基本上 .setRoot 和 .setController 会使应用程序崩溃。甚至 .load() 也这样做。
关于如何完成这项工作的任何建议?