JavaFX 位置未设置错误消息
2022-08-31 15:19:29
我在尝试关闭当前场景并在选择meneItem时打开另一个场景时遇到问题。我的主舞台编码如下:
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
在我关闭它并传递它之后,我确实设置了舞台。我想知道哪个部分出错了。
提前致谢。