我正在开发一个项目,我想使用外部 CSS 文件和 JavaFX 来设置 VBox 和窗格等的样式。
我使用下面的行尝试将样式表添加到相应的场景中:
scene.getStylesheets().add(getClass().getResource("/style.css").toExternalForm());
Run Code Online (Sandbox Code Playgroud)
这给了我空指针,我阅读了有关此问题的其他一些帖子,发现这通常是由于样式表不在尝试访问的类路径中。这是一个屏幕截图,您可以看到情况并非如此:
您会注意到有 Styles.css 和 style.css,我尝试了这两种方法以用于不同的故障排除目的
我还发现人们的建议说,如果它位于类路径中,则应该这样访问它:
scene.getStylesheets().add("style.css");
Run Code Online (Sandbox Code Playgroud)
然而这样做给了我
Nov 04, 2018 9:24:10 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "Styles.css" not found.
Run Code Online (Sandbox Code Playgroud)
我愿意接受任何建议,我正在使用 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> …Run Code Online (Sandbox Code Playgroud)