我在尝试关闭当前场景并在选择 menuItem 时打开另一个场景时遇到问题。我的主要阶段编码如下:
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Shop Management");
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("cartHomePage.fxml"));
Pane myPane = (Pane) myLoader.load();
CartHomePageUI controller = (CartHomePageUI) myLoader.getController();
controller.setPrevStage(primaryStage);
Scene myScene = new Scene(myPane);
primaryStage.setScene(myScene);
primaryStage.show();
}
当程序执行时,它会转到cartHomePage.fxml。从那里,我可以选择在选择菜单项时创建产品或创建类别。这是我的动作事件:
Stage prevStage;
public void setPrevStage(Stage stage){
this.prevStage = stage;
}
public void gotoCreateCategory(ActionEvent event) throws IOException {
Stage stage = new Stage();
stage.setTitle("Shop Management");
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("createCategory.fxml"));
Pane myPane = (Pane) myLoader.load();
Scene scene = new Scene(myPane);
stage.setScene(scene);
prevStage.close();
setPrevStage(stage);
stage.show();
}
//Method to change scene when menu item create product is on click
@FXML
public void gotoCreateProduct(ActionEvent event) throws IOException {
Stage stage = new Stage();
stage.setTitle("Shop Management");
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("creatProduct.fxml"));
Pane myPane = (Pane) myLoader.load();
Scene scene = new Scene(myPane);
stage.setScene(scene);
prevStage.close();
setPrevStage(stage);
stage.show();
}
但是,我只能切换一次舞台。例如,我的默认页面是 cartHomePage.fxml。当我运行程序时,首先我去创建产品阶段。在那之后,我再也不能去任何地方了。错误信息是:
java.lang.IllegalStateException: Location is not set.
and Null Pointer Exception
在我关闭它并传递它之后,我确实设置了舞台。我想知道哪个部分出了问题。
提前致谢。