java - 如何在同一个项目中找到文件的路径?
问题描述
我有 java fx 应用程序,我将它从纯 java 项目迁移到 maven,现在我的 .fxml 文件的路径有问题。正如您在下面看到的,我需要 Main.java 中的 sample.fxml。我尝试了不同的路径,但没有任何效果。你能看看吗?
这是我尝试过的:
System.out.println(getClass().getResource("src/main/java/sample/fxml/sample.fxml"));
System.out.println(getClass().getResource("/java/sample/fxml/sample.fxml"));
System.out.println(getClass().getResource("sample/fxml/sample.fxml"));
System.out.println(getClass().getResource("/sample/fxml/sample.fxml"));
System.out.println(getClass().getResource("fxml/sample.fxml"));
System.out.println(getClass().getResource("sample.fxml"));
System.out.println(getClass().getResourceAsStream("src/main/java/sample/fxml/sample.fxml"));
System.out.println(getClass().getResourceAsStream("/java/sample/fxml/sample.fxml"));
System.out.println(getClass().getResourceAsStream("sample/fxml/sample.fxml"));
System.out.println(getClass().getResourceAsStream("/sample/fxml/sample.fxml"));
System.out.println(getClass().getResourceAsStream("fxml/sample.fxml"));
System.out.println(getClass().getResourceAsStream("sample.fxml"));
这些中的每一个都返回null。
解决方案
我找到了答案。
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
当我们将 sample.fxml 移动到资源文件夹时正在工作。
推荐阅读
- html - 调整线性渐变路径 HTML/CSS 的宽度
- javascript - 如何使用canvasJs绘制图形?
- python - 如何继承父类的所有功能?
- dart - 在 Flutter 中使用 Stream/Sink
- django - Django:如何使用 DetailView 反转对象?
- rust - 返回包含引用的结果时“变量的寿命不够长”,但它的寿命确实足够长
- php - PHP doesnt want to send any emails from Hostinger hosting
- mysql - Flask SQL-Alchemy query is returning null for data that exists in my database. What could be the cause
- xml - Delete Duplicated Node From XML
- r - 一次将数据框中的所有 0 值替换为多行和多列的 1