javafx @font-face css error “loadStyleSheetUnprivileged”

时光总嘲笑我的痴心妄想 提交于 2020-01-17 13:59:25

问题


I'm trying to load a custom font in JavaFx css using this method

@font-face {
    font-family: 'Roboto';
    src: url('fonts/Roboto-Medium.ttf');
}

I've done every thing proper with the correct path but I'm getting this error

Nov 28, 2015 4:49:18 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
INFO: Could not load @font-face font [file:/C:/Users/RootUser/Desktop/Java8%20projects/RemoteViewer/out/production/JavaFxApplication/application/fonts/Roboto-Medium.ttf]

This is my project structure "screenshot"

Note I'm using intellij Idea as my IDE.Thanks in advance.


回答1:


This a bug in JavaFX (8). If there's a space in the font file's pathname, it won't load, and throw an exception, as it happened in with you.

This means that we shouldn't load fonts from a CSS file, unless we're sure the file path won't contain any spaces.

There is a better, safe way to load fonts (~first line inside main()):

Font.loadFont(getClass().getResourceAsStream("/font.ttf"), 16);



回答2:


For others who have come to this answer when searching:

The same error also occurs if you accidently leave out the quotes for the font name in a style, e.g.:

-fx-font-family: Monserrat;

is wrong, it needs to be

-fx-font-family: 'Montserrat';


来源:https://stackoverflow.com/questions/33973921/javafx-font-face-css-error-loadstylesheetunprivileged

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