How load css file in javafx8

家住魔仙堡 提交于 2019-12-05 20:47:15

You need an URL and call toExternalForm in order to load a css file into your project.
Use the ClassLoader for that:

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

For those who can't find css the way from accepted answer use classLoader:

getClass().getClassLoader().getResource("css/style.css").toExternalForm()

If you use maven it will find your css under resources/css/ dir.

Aolphn

In my case ,I want to load a css file from disk and I do it as this article said and it worked for me. code snippet as follow:

scene.getStylesheets().add("file:///E:/csse2002-7023/src/csse2002/block/world/main.css") 
firoz Miya

Simply add package name with .css file.

Your code will look like this:

scene.getStylesheets().add("sample/appCssFile.css");
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!