将css文件添加到javafx中的样式表 [英] adding css file to stylesheets in javafx

查看:1124
本文介绍了将css文件添加到javafx中的样式表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

语言:JavaFX

IDE:Netbeans

IDE: Netbeans

问题:我正在尝试添加css文件到样式表,但以下代码的第一行总是生成 NullPointerException

Problem: I'm trying to add a css file to the stylesheet, but the first line of the following code always generates a NullPointerException:

String css = this.getClass().getResource("double_slider.css").toExternalForm(); 
scene.getStylesheets().add(css);

我尝试用完整路径替换double_slider.css。 double_slider.css当前位于与进行此调用的类相同的包中。我也尝试过在 http://introjava.wordpress.com/2012/03/21/linking-a-css-style-sheet-to-javafx-scene-graph/ ,但没有成功。清理和构建也无济于事。

I've tried replacing "double_slider.css" with the full path. double_slider.css is currently located in the same package as the class that makes this call. I've also tried all of the variations found at http://introjava.wordpress.com/2012/03/21/linking-a-css-style-sheet-to-javafx-scene-graph/, with no success. Clean and build doesn't help either.

如果我将css文件放在转储.class文件的构建文件夹中,NullPointerException就会消失。但是后来css文件无法正常工作,因为它引用了我项目中的其他文件。

If I place the css file in the build folder where the .class files are dumped, the NullPointerException goes away. But then the css file does not work properly because it references other files in my project.

推荐答案

把你的 yourname.css 直接在 src 目录下的文件。

put your yourname.css file directly under src directory.

scene.getStylesheets().add("yourname.css")

清理并构建所需

这篇关于将css文件添加到javafx中的样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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