英文:
Where are .fxml files in Maven project with JavaFX dependency?
问题
我对Maven和JavaFX都还很新,所以我觉得肯定有一个简单的答案,但是我还没有找到。
我想为我的项目创建一个GUI,在IntelliJ中,我将项目设置为了一个Maven项目。我成功地通过添加javafx-fxml
和javafx-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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论