.fxml文件在具有JavaFX依赖的Maven项目中位于何处?

huangapple go评论108阅读模式
英文:

Where are .fxml files in Maven project with JavaFX dependency?

问题

我对Maven和JavaFX都还很新,所以我觉得肯定有一个简单的答案,但是我还没有找到。

我想为我的项目创建一个GUI,在IntelliJ中,我将项目设置为了一个Maven项目。我成功地通过添加javafx-fxmljavafx-controls来使GUI按预期工作。但是当我想尝试SceneBuilder时,我意识到找不到.fxml文件。

一些来源说要查看资源文件夹,但那里似乎是空的。另一种说法是有第二个src文件夹(?),但如果真是这样,我找不到它。

英文:

I'm pretty new to Maven and JavaFX so I'm assuming there's an easy answer, but I haven't been able to find it.

I want to create a GUI for my project, which I have as a maven project in IntelliJ. I was able to get a GUI to function as intended by adding javafx-fxml and javafx-controls. When wanting to try SceneBuilder I realized that the .fxml file was nowhere to be found.

Some sources say to look in the resources folder but that appears to be empty. Another says there is a second src folder(?) but if that's the case I can't find it.

答案1

得分: 1

假设你的包名是com.example

那么你的应用程序代码在这里 <项目根目录>\src\main\java\com\example\App.java

将你的FXML文件放在这里 <项目根目录>\src\main\resources\com\example\App.fxml

使用Java代码加载你的FXML文件

Parent root = FXMLLoader.load(getClass().getResource("App.fxml"));
Scene scene = new Scene(root);

App.java以及App.fxml位于同一个包中,因此getClass().getResource("App.fxml")可以正常工作。

英文:

Assume your package name is com.example

Then your Application code is here <project root>\src\main\java\com\example\App.java

Place your FXML here <project root>\src\main\resources\com\example\App.fxml

Load your FXML using Java code

Parent root = FXMLLoader.load(getClass().getResource("App.fxml"));
Scene scene = new Scene(root);

App.java as well as App.fxml are in the same package, therefore getClass().getResource("App.fxml") works fine

huangapple
  • 本文由 发表于 2020年9月18日 15:04:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63950805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定