[英]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.