【发布时间】:2019-09-01 15:34:52
【问题描述】:
我第一次尝试在 Maven 中使用 JavaFx。通过这个主题:链接IntelliJ can't recognize JavaFX 11 with OpenJDK 11,我配置了项目。但无论我做什么我都无法加载 fxml 文件,因为“getClass().getResource(path)”返回 null。
我更改了路径,以“/”开头,没有,更改包,创建包,删除包,更改模块信息中的引用,但这不起作用。
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);
}
}
可能有人知道这个问题并且可以帮助我。没有maven我没有任何问题。
决定
这样的路径:
String path = "/fxml/Input.fxml";
加上两个字符串到模块信息:
opens com.github.PavelKisliuk.controller to javafx.fxml;
exports com.github.PavelKisliuk.controller to javafx.fxml;
【问题讨论】:
-
您当前使用的是相对路径,正在针对
Main进行解析。您需要在这里使用绝对路径:"/fxml/Input.fxml"。请参阅getResource中的the documentation 了解更多信息。 -
检查,IntellIJ 用作运行程序的类路径的目录是否包含包含
Input.fxml文件的子目录fxml并使用路径"/fxml/Input.fxml"...