[英]Can't load fxml file by FXMLLoader (InvocationTargetException)
我第一次嘗試將JavaFx與Maven結合使用。 通過本主題:鏈接IntelliJ無法識別帶有我配置的項目的OpenJDK 11的JavaFX 11 。 但是無論如何我都無法加載fxml文件,因為“ getClass()。getResource(path)”返回null。
我更改了路徑,以“ /”開頭,並且沒有,更改了軟件包,創建了軟件包,刪除了軟件包,更改了模塊信息中的引用,但這是行不通的。
結構: https : //ibb.co/Hhwzk8b
module LogAggregator {
requires javafx.fxml;
requires javafx.controls;
opens fxml to javafx.fxml;
exports com.github.PavelKisliuk;
}
// ------------------------------------------------ ----
public class Main extends Application {
public void start(Stage primaryStage) throws Exception {
String path = "fxml/Input.fxml";
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource(path));
Parent fxmlMainWindow = fxmlLoader.load();
//start-up window
//-----------------------------------------------
Scene s = new Scene(fxmlMainWindow);
primaryStage.setScene(s);
primaryStage.show();
}
public static void main(String... args) {
launch(args);
}
}
可能有人知道這個問題,可以為我提供幫助。 沒有行家,我沒有任何問題。
決定
這樣的路徑:
String path = "/fxml/Input.fxml";
在模塊信息中加上兩個字符串:
opens com.github.PavelKisliuk.controller to javafx.fxml;
exports com.github.PavelKisliuk.controller to javafx.fxml;
您的Main類似乎在com.gihub.PavelKisliuk
包中,但資源位於fxml
下。 在這種情況下,您使用的相對路徑解析為不存在的com.gihub.PavelKisliuk/fxml/Input.fxml
。
解決方案:
希望這可以幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.