JavaFX. FXML loader can't find .fxml file in the project with modules

这一生的挚爱 提交于 2019-12-11 17:22:25

问题


I have a project with such structure:

I trying to load sample.fxml from the Main class using this code:

Parent root = FXMLLoader.load(Main.class.getResource("../../submodule/src/java/sample.fxml"));

but it doesn't work. The sample.fxml file code is:

<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>


<GridPane fx:controller="sample.Controller"
      xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10"    vgap="10">
</GridPane>

The problem is that FXML loader can't find this location. How to solve it?


回答1:


I would suggest to follow the basic maven package structure, like this:

src
 |--main
      |--java
      |--resource (put your FXML file into this folder)

Then the following should work:

Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("sample.fxml"));

You can also put your FXML file into a subfolder:

... = FXMLLoader.load(getClass().getClassLoader().getResource("layouts/sample.fxml"));


来源:https://stackoverflow.com/questions/55225349/javafx-fxml-loader-cant-find-fxml-file-in-the-project-with-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!