javaFX构建路径MVC fxml的问题

javaFX构建路径MVC fxml的问题,javafx,fxml,Javafx,Fxml,我目前正在使用netbeans 7.2在JavaFX2.1中开发一个管理员构建 我有以下问题: 我正在MVC模式中开发这个特殊的工具,所以我创建了3个包,分别称为Model、view和Controller 我的问题是,在netbeans中构建项目时,它只会读取应该在视图包中的文件(如果它们在视图包之外)。让我给你一个上下文路径: .../administradorInfinix/view/ .../administradorInfinix/controller/ .../administrado

我目前正在使用netbeans 7.2在JavaFX2.1中开发一个管理员构建

我有以下问题:

我正在MVC模式中开发这个特殊的工具,所以我创建了3个包,分别称为Model、view和Controller

我的问题是,在netbeans中构建项目时,它只会读取应该在视图包中的文件(如果它们在视图包之外)。让我给你一个上下文路径:

.../administradorInfinix/view/
.../administradorInfinix/controller/
.../administradorInfinix/model
因此,如果fxml文件位于视图包之外,它将只读取与视图相关的fxml文件(
../administradorInfinix/

这是我设置文件地址的地方:

    private void irInicioSesion() {
    try {
        replaceSceneContent("InicioSesion.fxml");
    } catch (Exception ex) {
        Logger.getLogger(AdministradorINFINIX.class.getName()).log(Level.SEVERE, null, ex);
    }
}
您可以看到文件名为
iniciosion.fxml
,它应该在视图包中,但如果是这种情况,它将不会加载

这是我用来搜索fxml文件的ReplaceScenContent:

private Parent replaceSceneContent(String fxml) throws Exception {
    Parent page = (Parent) FXMLLoader.load(AdministradorINFINIX.class.getResource(fxml), null, new JavaFXBuilderFactory());
    Scene scene = stage.getScene();
    if (scene == null) {
        scene = new Scene(page,548,416);
        //scene.getStylesheets().add(AdministradorINFINIX.class.getResource("demo.css").toExternalForm());
        stage.setScene(scene);
    } else {
        stage.getScene().setRoot(page);
    }
    stage.sizeToScene();
    return page;
}
这是它在尝试运行时给我的错误(它构建得很好,但不会运行)

110线在哪里

  replaceSceneContent("InicioSesion.fxml");
第126行是

Parent page = (Parent) FXMLLoader.load(AdministradorINFINIX.class.getResource(fxml), null, new JavaFXBuilderFactory());

我希望您能帮助我解决这个问题。

您需要使用FXML文件的URL调用方法FXMLLoader#setLocation。有关如何加载FXML文件的示例,请查看以下源代码:


FXMLLoader无法找到.fxml文件。 问题是对Class.getResource()的调用返回null。 FXMLLoader.load(null)引发的异常非常容易引起误解,它应该是ArgumentNullException之类的异常。 您可以通过指定完整的包路径来解决加载资源文件的问题,在我的情况下,这样的调用可以工作:

    FXMLLoader loader = new FXMLLoader(new Employee().getClass().getResource("/de/mycompany/mypackage/view/loginform.fxml"));

我希望这有帮助。

修复了答案中的链接。
    FXMLLoader loader = new FXMLLoader(new Employee().getClass().getResource("/de/mycompany/mypackage/view/loginform.fxml"));