我有一个 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"));
关于java - 从子目录膨胀 FXML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40141243/