- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题描述:
我正在使用 IntelliJ IDEA 开发 JavaFX 应用程序,并遇到应用程序找不到放置在资源文件夹中的 CSS 文件的问题。
在我的元素中,有一个 style.css
文件位于 src/main/resources/view
下。尽管如此,当尝试使用 getClass().getResource("/view/style.css")
将其加载到我的 JavaFX 应用程序中时,它始终返回 null
。
相关代码实现:
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());
}
}
遇到的问题:
方法 1( scene.getStylesheets().add("src/main/resources/view/style.css");
):
WARNING: Resource "src/main/resources/view/style.css" not found.
方法 2( scene.getStylesheets().add(getClass().getResource("/view/style.css").toExternalForm());
):
null
,导致尝试调用 .toExternalForm()
时出错。其他信息:
正确打印元素目录,确认工作目录。
我已验证文件路径中没有拼写错误。
CSS文件肯定存在于指定的目录中。
CSS 文件不在目标输出文件夹中。
我使用 IntelliJ IDEA 作为我的元素结构,而不是 Maven 或 Gradle。以下是我的.iml
的相关内容文件:
<?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>
问题:
是什么原因导致无法找到 style.css
文件,如何正确地将其加载到我的 JavaFX 应用程序中?
任何见解或建议将不胜感激!
尝试:
我尝试了多种方法来解决 getResource()
的问题 始终返回 null
。以下是我所采取的步骤的摘要:
在不同包的其他类中复制了该问题,在 getResource()
中遇到了同样的问题。 返回 null
。
尝试了各种字符串来定位 CSS 文件,包括:
"style.css"
"/style.css"
"view/style.css"
"/view/style.css"
将 CSS 文件直接移至 resources
文件夹。
尝试了不同的 CSS 文件名和创建这些文件的方法。
重新启动 IntelliJ IDEA。
重建整个软件。
使 IntelliJ IDEA 中的缓存失效并重新启动(为了确保安全,我在每次更改后都会执行此操作)。
我确实尝试在其他包的其他类中复制它。 getRecource() 始终返回 null。
到设置 -> 编译器 -> 我添加的资源模式 !?*.css
getClass().getClassLoader().getResource("/view/style.css");
将资源文件夹直接移动到元素文件夹/src/main/resources ->/resources
最佳答案
To Settings -> Compiler -> Resource patterns I've added !?*.css"
你不想这样做。
将 .css
文件视为 NOT 资源。这与您想要的相反。
请注意,(我认为)编译器设置仅用于处理标记为 Sources
的目录下的文件,并且不会影响标记为 Resources
的文件。根据我的测试,我标记为 Resources
的目录下的 .css
文件始终被复制到输出目录,无论 Compiler | 是什么。资源模式
设置。因此,这可能不是问题的根本原因。
工作示例
我尝试过:
默认情况下(没有其他更改),我放入创建的资源目录中的 .css
文件被复制到构建输出目录,并且可以通过 getResource
找到code> 按预期查找。
构建后:
<projectdir>/out/production/<modulename>/
包含这些文件:
Main.class
styles.class
/src/Main.java
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
System.out.println(Main.class.getResource("/styles.css"));
}
}
/resources/styles.css
.root {
-fx-base: mistyrose;
}
输出
Hello world!
file:/C:/dev/resourcetest/out/production/resourcetest/styles.css
设置 |编译 |资源模式
这些是使用 Idea 2023.2.4 (UE) 的新 Java 元素的默认值,我没有更改它们:
!?*.java
!?*.form
!?*.class
!?*.groovy
!?*.scala
!?*.flex
!?*.kt
!?*.clj
!?*.aj
推荐
使用构建工具(例如 Maven 或 Gradle),遵循 Maven standard layout ,将构建元素与 IDE 同步通常是比在 IDE 中手动配置元素更好的方法。
关于IntelliJ IDEA 中的 JavaFX 应用程序无法在资源中找到 CSS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77465864/
有人告诉我,如果我只有一个“东西”,比如家(不是多个家),我应该在 routes.rb 中使用资源 :home,而不是资源 :home。但是当我查看路由时,POST 函数似乎想要 home#creat
Activity 开始。这些代码框架顺利通过。 // Initialize array adapters. One for already paired devices and //
资源 search-hadoop.com search-hadoop.com索引所有邮件列表,非常适合历史搜索。当你遇到问题时首先在这里搜索,因为很可能有人已经遇到了你的问题。 邮件列表 在A
我是 WPF 的新手,正在努力使用位于单独程序集中的样式。这就是我正在做的:- 我有一个带有\Themes 文件夹的类库项目,其中包含一个“generic.xaml”,它合并了\Themes 内的子文
我正在编写一个使用虚拟树状文件结构的插件。基本上它就像一个包含文件的标准文件系统,区别在于这些文件实际上并不存在于文件系统中的特定位置,而只是 java 对象。 这些当前由使用 SettingProv
如果我在 XAML 中使用以下内容,我会收到错误消息: 错
我正在使用 laravel 资源来获取 api 的数据: return [ 'id' => $this->id, 'unread' =>
我有以下 pom.xml: 4.0.0 mycompany resource-fail 0.0.1-SNAPSHOT BazBat
许多GDI +类都实现IDisposable,但是我不确定何时应该调用Dispose。对于使用new或静态方法(例如Graphics.CreateGraphics)创建的实例来说,这很明显。但是,由属
我正在构建一组 RESTful 资源,其工作方式如下:(我将使用“people”作为示例): 获取/people/{key} - 返回一个人对象 (JSON) GET/people?first_nam
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一个使用 $resource 的简单 Controller : var Regions = $resource('mocks/regions.json'); $scope.regions =
在 Azure 门户中,如何查看不同资源之间的依赖关系。我特别想查看哪些资源正在使用我要删除的存储。 最佳答案 您可以使用应用程序洞察应用程序 map 来执行此操作: 您还可以打开存储帐户的日志记录:
我正在使用 ionic 生成资源(图标和启动画面)。我正在使用 ionic v2.1.0 和 cordova v6.4.0。 到目前为止我一直在使用(它在以前的版本中工作): cordova plat
是否可以使用 Assets 包含子文件夹中的文件? 示例:[base_url]/assets/css/pepper-grinder/jquery-ui-1.8.11.custom.min.css 最佳
我正在阅读一些尝试教授 Android 开发的书。在书中,作者概述了 res/下的一些目录。他提到 res/menu 包含基于 XML 的菜单规范。他还提到了保存“通用文件”的 res/raw。当我创
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我在服务器上使用 express-resource。在我的 AngularJS Controller 中: var User = $resource('/services/users/:use
因此,每当我运行我的应用程序时,它都会立即崩溃并给出以下错误: No package identifier when getting value for resource number 0x00000
对于我正在创建的(网络)应用程序,我需要使用基本身份验证在我的 UIWebView 中加载页面。 现在设置我使用的授权 header : NSString *result = [NSString st
我是一名优秀的程序员,十分优秀!