Issue Description:
I am developing a JavaFX application using IntelliJ IDEA and encountering an issue where the application does not locate a CSS file placed in the resources folder.
在我的项目中,有一个100文件位于101下.尽管如此,当try 将其加载到我的带有102的JavaFX应用程序中时,它始终返回103.
Relevant Code Implementation:个
public class MainGui extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane borderPane = new BorderPane();
borderPane.setCenter(new DocumentSelectionView());
Scene scene = new Scene(borderPane, 1400, 900);
// Attempting to load the CSS file
scene.getStylesheets().clear();
System.out.println(System.getProperty("user.dir")); // Prints the project directory
// Different methods tried to add the stylesheet
// Method 1
scene.getStylesheets().add("src/main/resources/view/style.css");
// Method 2
scene.getStylesheets().add(getClass().getResource("/view/style.css").toExternalForm());
}
}
Problems Encountered:个
-
Method 1(100):
- Results in the warning:
WARNING: Resource "src/main/resources/view/style.css" not found.
- Results in the warning:
-
Method 2(100):
- 返回100,导致在try 呼叫101时出错.
Additional Information:
-
项目目录打印正确,确认了工作目录.
-
我已经验证了文件路径中没有印刷错误.
-
Css文件肯定存在于指定的目录中.
-
Css文件不在目标输出文件夹中.
-
我正在使用IntelliJ IDEA作为我的项目 struct ,而不是Maven或Gradle.以下是我的100文件的相关内容:
<?xml version="1.0" encoding="UTF-8"?> <module version="4"> <component name="AdditionalModuleElements"> <content url="file://$MODULE_DIR$" dumb="true"> <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" /> <excludeFolder url="file://$MODULE_DIR$/target" /> </content> </component> </module>
Question:个
定位100文件失败的原因可能是什么,以及如何正确地将其加载到我的JavaFX应用程序中?
如有真知灼见或建议,我们将不胜感激!
Attempts:个
我try 了几种方法来解决100始终返回101的问题.以下是我采取的步骤的摘要:
-
在不同包的其他类中复制了该问题,遇到了相同的问题,100返回101.
-
已try 各种字符串来定位该css文件,包括:
-
100
-
100
-
100
-
100
-
-
已将css文件直接移到100文件夹中.
-
try 使用不同的css文件名和创建这些文件的方法.
-
已重新启动IntelliJ Idea.
-
重建了整个软件.
-
使IntelliJ Idea中的缓存无效,并执行重新启动(我在每次更改后都会这样做,只是为了确保).
-
我确实try 过在其他类别的其他包中复制它.GetRecource()总是返回空.
-
到设置->;编译器->;我已添加
!?*.css
个资源模式 -
getClass().getClassLoader().getResource("/view/style.css");
个 -
直接在项目文件夹/src/main/Resources->;/Resources中移动资源文件夹