我有一个Launcher类,我想用它打开一个新窗口。
从发射器的主要部分,我打电话:
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();
}
}
但是我收到了java.lang.IllegalStateException: Location is not set.
,我正在呼叫fxmlLoader.load()
。我的项目文件结构如下:
我已尝试输入FXML文件的绝对文件路径,但仍然没有运气。
任何人都可以帮助我理解在JavaFX(具有多个阶段)中夸大FXML的一般原则是什么,或者指出他们遇到的好资源。
干杯。
答案 0 :(得分:1)
我知道这是一个老问题,但也许可以帮助别人。
您必须编写fxml的所有路径。 在你的情况下是:
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/client/fxml/ChatList.fxml"));
另一个例子: Youproject / Src的/ parentpackage / childpackage / fxmlToGet.fxml
如果你想在childpackage中获取fxml,你必须写:
FXMLLoader(getClass().getResource("/parentpackage/childpackage/fxmlToGet.fxml"));