JavaFX位置未设置错误消息 [英] JavaFX Location is not set error message
问题描述
我在选择menuItem时尝试关闭当前场景并打开另一个场景时遇到问题。我的主要阶段编码如下:
I have problem when trying to close current scene and open up another scene when menuItem is selected. My main stage is coded as below:
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。从那里,我可以选择在选择菜单项时创建产品或创建类别。这是我的行动事件:
When the program is executed, it will go to the cartHomePage.fxml. From there, I can select to go to create product or create category when the menu item is selected. Here is my action event:
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。当我运行程序时,首先我去创建产品阶段。在那之后,我不能再去任何地方了。错误消息是:
However, I can only switch the stage once. For example, my default page is cartHomePage.fxml. When I run the program, first I go to create product stage. After that, I cannot go to anywhere any more. The error message is:
java.lang.IllegalStateException: Location is not set.
and Null Pointer Exception
我关闭它并传递它后确实设置了舞台。我想知道哪个部分出了问题。
I did set the stage after I close it and pass it around. I wonder which part went wrong.
提前致谢。
推荐答案
我有这个问题,发现了这篇文章。我的问题只是文件名问题。
I had this problem and found this post. My issue was just a file name issue.
FXMLLoader(getClass().getResource("/com/companyname/reports/" +
report.getClass().getCanonicalName().substring(18).replaceAll("Controller", "") +
".fxml"));
Parent root = (Parent) loader.load();
我有一个xml,这一切都来自我,我确保我的班级是一样的因为fxml文件少了字控制器。
I have an xml that this is all coming from and I have made sure that my class is the same as the fxml file less the word controller.
我搞砸了子字符串,所以路径错了......确定它修复了它起作用的文件名后确定了。
I messed up the substring so the path was wrong...sure enough after I fixed the file name it worked.
总而言之,我认为问题是文件名不正确或路径错误。
To make a long story short I think that the problem is either the filename is named improperly or the path is wrong.
附加:
我已搬到Maven项目。非Maven方式是将所有内容都放在项目路径中。下面的答案中列出的Maven方式在开始时有点令人沮丧,但我对我的代码进行了如下更改:
ADDITION: I have since moved to a Maven Project. The non Maven way is to have everything inside of your project path. The Maven way which was listed in the answer below was a bit frustrating at the start but I made a change to my code as follows:
FXMLLoader loader = new FXMLLoader(ReportMenu.this.getClass().getResource("/fxml/" + report.getClass().getCanonicalName().substring(18).replaceAll("Controller", "") + ".fxml"));
这篇关于JavaFX位置未设置错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!