找不到将 CSS 导入 JavaFX 的源

Source not found importing CSS to JavaFX

我正在开发一个项目,我想在其中使用带有 JavaFX 的外部 CSS 文件来设置 VBox 和窗格等的样式。

我已经使用下面的行尝试将样式 sheet 添加到相应的场景中:

scene.getStylesheets().add(getClass().getResource("/style.css").toExternalForm());

这给了我空指针,我阅读了有关此问题的其他一些帖子,发现这通常是由于样式 sheet 不在它试图成为的 class 路径中从访问。这是一个屏幕截图,可以让您看到情况并非如此:

您会注意到有 Styles.css 和 style.css,出于不同的故障排除目的,我都尝试了两者

我还发现有人建议说如果它在 class 路径中,应该这样访问它:

scene.getStylesheets().add("style.css");

但是这样做给了我

Nov 04, 2018 9:24:10 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "Styles.css" not found.

我愿意接受任何建议,我正在使用 maven 在 IntelliJ 中工作。

编辑:这是用于进一步调查的 pom 文件 -

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.almasb</groupId>
<artifactId>CashMachine</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <source.version>1.8</source.version>

    <!-- plugins -->
    <maven.compiler.version>3.3</maven.compiler.version>
    <maven.shade.version>2.4.2</maven.shade.version>

    <!-- dependencies -->
</properties>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.version}</version>
            <configuration>
                <source>${source.version}</source>
                <target>${source.version}</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>${maven.shade.version}</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.almasb.atm.CashMachineApp</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

如果您的 css 文件与您的 class 位于同一个包中:

scene.getStylesheets().add(getClass().getResource("style.css").toString());

Maven 约定使用 standard directory layout,除非另有配置,否则要求您遵循它。标准目录布局所做的一件事是将 Java 源文件 (.java) 与资源文件(不是 .java 的所有内容)分开。看起来像这样:

|--projectDir/
|  |--src/
|  |  |--main/
|  |  |  |--java/
|  |  |  |--resources/

源文件在 src/main/java 中,资源文件在 src/main/resources 中。

构建项目时,Maven 将编译源文件并将 .class 文件放入 projectDir/target/classes。它还会将任何资源文件从 src/main/resources 复制到 target/classes 中。默认情况下,src/main/java 中的任何非源文件都将被忽略。您提到您已将 styles.css 文件放在 src/main/java 中,因此当您 运行 您的项目时它不在 target/classes 中。

当您执行项目时,目录 target/classes(以及任何依赖项)将放在类路径中。但是因为你的 styles.css 文件没有被复制到 target/classes 中,所以它没有包含在类路径中,你最终在尝试引用它时得到 NullPointerExceptions。

解决办法是将styles.css文件移动到src/main/resources目录下。看起来您目前在 src/main/java/rocks/zipcode/atm 下拥有它。如果你想保持资源文件不变 "package" 将它移动到 src/main/resources/rocks/zipcode/atm。然后当你重建你的项目时,它应该被复制到 target/classes 目录。

然后您可以通过多种方式引用它。

第一种方法是使用 getClass().getResource(...)。如果您传递绝对路径(以 / 开头),它将从类路径的根目录中查找资源。如果您不传递绝对路径(不以 / 开头),它将查找相对于 Class 位置 的资源 (在这种情况下,Class 是由 getClass() 返回的)。这意味着,由于您是从 rocks.zipcode.atm.CacheMachineApp 中获取资源,您可以:

  • getClass().getResource("/rocks/zipcode/atm/styles.css"),或
  • getClass().getResource("styles.css")

另一种选择是使用 getStylesheets() 文档中定义的行为:

Gets an observable list of string URLs linking to the stylesheets to use with this scene's contents.

The URL is a hierarchical URI of the form [scheme:][//authority][path]. If the URL does not have a [scheme:] component, the URL is considered to be the [path] component only. Any leading '/' character of the [path] is ignored and the [path] is treated as a path relative to the root of the application's classpath.

如您所见,当没有方案时(例如file:https:等),它将查找相对于root 的资源类路径。因为它总是相对于根你需要传递绝对路径(但在这种情况下它不需要前导 /)。

getStylesheets().add("rocks/zipcode/atm/styles.css");

重要说明:确保路径完全正确。打包在 JAR 文件中时区分大小写。当未打包在 JAR 文件中时,它可能区分大小写,也可能不区分大小写,但这取决于底层文件系统。无论哪种方式,最好匹配实际路径的大小写,以免出现问题。

对我有用的是在 'path from content' 的开头添加“//” 像这样:"//src/practice/Viper.css"