gpt4 book ai didi

java - 未知路径 FXML 文档

转载 作者:行者123 更新时间:2023-12-01 10:55:54 25 4
gpt4 key购买 nike

我们正在使用 JavaFX 编写 Java 应用程序。此时我们有3种不同的形式:

  • 登录
  • 游戏窗口
  • 注册

对于下一次迭代,我们想要实现注册表单,但我们收到 IOException 错误未知路径

关于这段代码:

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.fxmlFXMLLoader.fxml 时,上述代码有效。

当我改变

mainroot = (Parent)registrationLoader.load(this.getClass().getResource("FXMLRegistration.fxml").openStream());

mainroot = (Parent)registrationLoader.load(Paths.get("src/hackattackfx/FXMLRegistration.fxml").toUri().toURL());

source

我在调试器输出中获得了绝对路径,当我在终端中将其与 file 命令一起使用时,这是正确的。

我希望有人可以帮助我们解决这个错误。

提前致谢!

编辑

我将一些代码更改为以下内容:

FXMLLoader registrationLoader = new FXMLLoader(getClass().getResource("/FXMLRegistration.fxml"));
mainroot = (Parent)registrationLoader.load();

但这将返回 IllegalStateException:未设置位置。当我在 /FXMLRegistration.fxml 之前删除 / 时,我会在 catch block 中打印文件的完整路径:

file:/Users/juleskreutzer/Documents/github/PTS3/HackAttackFX/dist/run1793658053/HackAttackFX.jar!/hackattackfx/FXMLRegistration.fxml

此外,更改 src/hackattackfx/FXMLRegistration.fxml 的路径也会出现 IllegalStateException:位置未设置。

项目结构

我们在应用程序中使用不同的包。所有这些包都在默认包中:hackattackfx

默认包中的包有:

  • 默认包
    • 异常(exception)
    • 接口(interface)
    • 枚举
    • 资源
    • 模板
  • JSON 包

我的 FXML 文档位于默认包 (hackattackfx) 中。如果不是 100% 清楚我是如何安排文件的,请查看 my Github repo

最佳答案

所以,我很好奇找出根本原因,我克隆了存储库,发现实际问题是以下错误以及OP在问题中发布的错误

Caused by: java.lang.NullPointerException at hackattackfx.FXMLRegistrationController.initialize(FXMLRegistrationController.java:67)

这意味着在 Controller Pane 中为空。

这是因为 fxml 缺少 fx:id 声明。

fx:id="pane" 添加到 FXMLRegistration.fxml 的 AnchorPane 声明中,一切应该会正常工作。

关于java - 未知路径 FXML 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33613309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com