从子目录膨胀 FXML

Inflating FXML from subdirectory

我有一个启动器 class,我想用它来打开一个新的 window。

我在 Launcher 的 main 中调用:

ChatList chatList = new ChatList(communicator);

ChatList 的构造函数调用方法 showChatList(),我在其中尝试扩充 FXML 文档:

    private void showChatList() {
    try {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("fxml/ChatList.fxml"));
        Parent root = (Parent) fxmlLoader.load();
        Stage stage = new Stage();
        stage.setScene(new Scene(root));
        stage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

然而,我在呼叫 fxmlLoader.load() 时收到 java.lang.IllegalStateException: Location is not set.。我的项目文件结构如下:

我已经尝试输入 FXML 文件的绝对文件路径,但仍然没有成功。

任何人都可以帮助我理解在 JavaFX(具有多个阶段)中膨胀 FXML 背后的一般原则是什么,或者给我指出他们遇到的一个很好的资源。

干杯。

我知道这是一个老问题,但也许可以帮助别人。

你必须写出fxml的所有路径。 你的情况是:

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/client/fxml/ChatList.fxml"));

另一个例子: Youproject/Src/parentpackage/childpackage/fxmlToGet.fxml

如果你想获取子包中的fxml,你必须这样写:

FXMLLoader(getClass().getResource("/parentpackage/childpackage/fxmlToGet.fxml"));