我们正在使用JavaFX编写Java应用程序。在这个时候,我们有3种不同的形式:未知路径FXML文档
- 登录
- 游戏窗口
- 注册
对于我们的下一个迭代,我们要实现的登记表,但我们得到的IOException异常错误Unknown Path
这是关于这段代码:
FXMLLoader registrationLoader = new FXMLLoader();
try{
mainroot = (Parent)registrationLoader.load(this.getClass().getResource("FXMLRegistration.fxml").openStream());
Stage registrationStage = new Stage();
Scene scene = new Scene(mainroot);
registrationStage.setScene(scene);
registrationStage.setTitle("Register your account");
registrationStage.show();
} catch(IOException ex)
{
System.out.print(ex.getMessage());
}
当我将FXMLRegistration.fxml
更改为FXMLDocument.fxml
或FXMLLoader.fxml
时,上述代码正在工作。
当我改变
mainroot = (Parent)registrationLoader.load(this.getClass().getResource("FXMLRegistration.fxml").openStream());
到
mainroot = (Parent)registrationLoader.load(Paths.get("src/hackattackfx/FXMLRegistration.fxml").toUri().toURL());
我得到在调试器输出的绝对路径,当我在终端的file
命令使用它这是正确的。
我希望有人能帮助我们解决这个错误。
在此先感谢!
编辑
我改变了一些代码,如下:
FXMLLoader registrationLoader = new FXMLLoader(getClass().getResource("/FXMLRegistration.fxml"));
mainroot = (Parent)registrationLoader.load();
但这会返回一个IllegalStateException:位置没有设置。 当我/FXMLRegistration.fxml
之前删除/
,我让我的catch块打印文件的完整路径:
文件:/Users/juleskreutzer/Documents/github/PTS3/HackAttackFX/dist/run1793658053/HackAttackFX.jar !/hackattackfx/FXMLRegistration.fxml
而且改变路径src/hackattackfx/FXMLRegistration.fxml
会给IllegalStateException异常:位置未设置。
项目结构
我们在应用程序中使用不同的封装。所有这些包是默认的包内:hackattackfx
在默认包的包:
- 默认套餐
- 例外
- 接口
- 枚举
- 资源
- 模板
- JSON包
我FXML文件都位于默认包(hackattackfx)。如果不是100%清楚,我怎么安排我的文件,请看看my Github repo
你的问题不是很清楚(对我)。当你使用'Paths.get(“src/hackattackfx/FXMLRegistration.fxml”).toUri()。toURL()'它是否工作?你的目标是使用'Paths.get()'?如果是,为什么? – ItachiUchiha
Paths.get()不起作用,它将打印到我的调试器的路径,以便它可以放到我的catch块中。如果可能,我想使用getClass()。getResource(“X”)。openStream() – Jules
如果文件存在于类路径中,则应该使用'getClass()。getResource()'。在这种情况下,Paths.get()没有任何意义。 – ItachiUchiha