首页 > 解决方案 > jar中的Javafx无法打开新场景

问题描述

在我的主控制器应用程序中,我尝试打开新场景

public void runReportParserWindow() throws IOException {

    System.out.println("Open report parser window");
    FXMLLoader fxmlLoader = new FXMLLoader();      
    fxmlLoader.setLocation(getClass().getResource("../views/reportWindow.fxml"));
    ControllerReportParser controller = new ControllerReportParser(getPrimaryStage(), getMainApp());
    controller.registerObserver(this);
    fxmlLoader.setController(controller);
    Scene scene = new Scene(fxmlLoader.load());
    Stage stage = new Stage();
    stage.setTitle("Get data for report");
    stage.setScene(scene); //scene
    stage.initModality(Modality.WINDOW_MODAL);
    stage.initOwner(getPrimaryStage());

    stage.show();
}

如果我在 Intellij idea 中运行应用程序 - 所有窗口都可以正常打开。当我创建 JAR 文件(工件)时,我无法打开除主窗口之外的任何窗口。显示了主要场景,但没有显示其他场景。工件的设置 在此处输入图像描述

我究竟做错了什么?

标签: intellij-ideajavafx

解决方案


非常感谢 James_D 我使用相对路径来加载 fxml 文件。现在我将视图文件夹替换为控制器并更改代码

fxmlLoader.setLocation(getClass().getResource("../views/reportWindow.fxml"));

fxmlLoader.setLocation(getClass().getResource("views/reportWindow.fxml"));

一切正常。


推荐阅读