将您的文件移动到src/main/resources 并添加您的css 文件:
scene.getStylesheets().add(getClass().getClassLoader().getResource("stylesheet.css").toExternalForm());
好吧,如果你想从 jar 中运行它,那么将 stylesheet.css 更改为 stylesheet.bss (binary css),打包你的应用程序:
mvn clean compile jfx:build-jar
然后运行你的 jar。
java -jar app.jar
我有一个丑陋的 hack 使它有点可用(我正在使用 Netbeans,令人惊叹的 maven 完整性):
我在src/main/resources 目录中创建了一个project.properties 文件,
file_css= ${file_css} // by default I use *.css file.
并使其可过滤,在我的POM 文件中:
...
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version> 1.5 </version>
<configuration>
....
</configuration>
</plugin>
</plugins>
</build>
...
并创建两个maven 配置文件,一个用于dev,另一个用于production(打包到jar):
<profiles>
<profile>
<id>production</id>
<properties>
<file_css>stylesheet.bss</file_css>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<file_css>stylesheet.css</file_css>
</properties>
</profile>
</profiles>
所以,你可以像这样加载你的css 文件:
scene.getStylesheets().add(getClass().getClassLoader().getResource(ResourceBundle.getBundle("project").getString("file_css")).toExternalForm());
我使用production 配置文件进行打包,dev 用于通常的操作,例如compile, test, run。
编辑:
一个完整的例子托管在github。