找不到将CSS导入JavaFX的源 [英] Source not found importing CSS to JavaFX

查看:92
本文介绍了找不到将CSS导入JavaFX的源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a project where I'd like to use an external CSS file with JavaFX to style the VBox and panes and such.

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

I've used the line below to try to add the style sheet to the respective scene:

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

这给了我空指针,我阅读了有关此问题的其他文章,发现这通常是由于样式表不在试图从其访问的类路径中.这是一个屏幕截图,可让您看到情况并非如此:

This gave me null pointers, I read some other posts on this issue and found that this is often due to the stylesheet not being in the class path that it is trying to be accessed from. Here is a screenshot that allows you to see that this is not the case:

您会注意到这里有Styles.css和style.css,我为不同的故障排除目的都尝试过

You'll notice there is Styles.css and style.css, I tried both for different troubleshooting purposes

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

I also found suggestions from people saying that if it is in the class path, that it should be accessed as such:

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中工作.

I'm open to any suggestions, I'm working in IntelliJ using maven.

这是待进一步调查的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>

推荐答案

Maven约定使用

Maven convention uses a standard directory layout and, unless otherwise configured, requires that you follow it. One of the things the standard directory layout does is separate Java source files (.java) from resource files (everything not .java). It looks like this:

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

源文件放入src/main/java,资源文件放入src/main/resources.

The source files go in src/main/java and the resource files go in src/main/resources.

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

When you build your project Maven will compile the source files and put the .class files in projectDir/target/classes. It will also copy any resource files from src/main/resources into target/classes as well. By default, any non-source files in src/main/java will be ignored. You mention you had put your styles.css file in src/main/java and thus it is not in target/classes when you run your project.

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

When you execute the project the directory target/classes (as well as any dependencies) is put on the classpath. But because your styles.css file wasn't copied into target/classes it is not included in the classpath and you end up getting NullPointerExceptions when trying to reference it.

解决方案是将styles.css文件移动到src/main/resources目录中.看起来您当前在src/main/java/rocks/zipcode/atm下拥有它.如果要将资源文件保留在相同的包"中,请将其移至src/main/resources/rocks/zipcode/atm.然后,在重建项目时,应将其复制到target/classes目录.

The solution is to move the styles.css file into the src/main/resources directory. It looks like you currently have it under src/main/java/rocks/zipcode/atm. If you want to keep the resource file in the same "package" move it to src/main/resources/rocks/zipcode/atm. Then it should be copied to the target/classes directory when you rebuild your project.

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

Then you can reference it a couple of ways.

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

The first way is using getClass().getResource(...). If you pass an absolute path (starts with a /) it will look for the resource from the root of the classpath. If you don't pass an absolute path (doesn't start with a /) it will look for the resource relative to the Class's location (in this case, the Class is the one returned by getClass()). This means, since you're getting the resource from within rocks.zipcode.atm.CacheMachineApp, you could either do:

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

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

The other option is to use the behavior defined in the documentation of getStylesheets():

获取可观察到的字符串URL列表,这些URL链接到用于此场景内容的样式表.

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

URL是格式为[scheme:] [//authority] [path]的分层URI.如果URL没有[scheme:]组件,则该URL仅被视为[path]组件. [path]的任何前导'/'字符都将被忽略,并且[path]被视为相对于应用程序类路径根目录的路径.

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:等)时,它将查找相对于类路径的的资源.由于它总是相对于根目录,因此您需要传递绝对路径(但在这种情况下,它不需要前导/).

As you can see, when there is no scheme (e.g. file:, https:, etc.) it will look for the resource relative to the root of the the classpath. Since it is always relative to the root you need to pass the absolute path (but in this case it doesn't require a leading /).

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

重要说明:请确保您正确地获得路径 .打包在JAR文件中时,区分大小写.如果未打包到JAR文件中,则可能区分大小写,也可能不区分大小写,但这取决于基础文件系统.无论哪种方式,最好都匹配实际路径的大小写,以免出现任何问题.

Important Note: Make sure you get the path completely correct. It is case sensitive when packaged in a JAR file. It may or may not be case sensitive when not packaged in a JAR file, but that is dependent on the underlying file system. Either way, it is best to match the case of the actual path in order to avoid any problems.

这篇关于找不到将CSS导入JavaFX的源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆