How load css file in javafx8

不想你离开。 提交于 2020-01-23 09:51:07

问题


javafx 8 this code is not working

  scene.getStylesheets().add("appCssFile.css");

give Exception

Mar 25, 2014 12:21:20 PM com.sun.javafx.css.parser.CSSParser reportException
WARNING: Please report java.lang.NumberFormatException at:
Mar 25, 2014 12:21:20 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "appCssFile.css" not found. 

How can i load css ?


回答1:


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());



回答2:


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.




回答3:


Simply add package name with .css file.

Your code will look like this:

scene.getStylesheets().add("sample/appCssFile.css");



回答4:


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") 


来源:https://stackoverflow.com/questions/22627579/how-load-css-file-in-javafx8

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