JavaFX Embed Custom Font Not Working

两盒软妹~` 提交于 2019-12-23 03:09:31

问题


I'm using JavaFX version 8.0.40-b27 and trying to embed a custom/external font via CSS. I've also tried programmatic approaches, all of which have failed. A System.out.print of "font" returns null, which I suspect to be the cause.

Java:

Font font = Font.loadFont( Main.class.getClassLoader().getResourceAsStream( "application/stratum.ttf"), 10);
System.out.println(font); // Prints "null"
nowPlayingTitle.setFont(font);

CSS:

@font-face {
    font-family: stratum;
    src: url('stratum.ttf');
}

.text{
 	-fx-font-family: "stratum", "Segoe UI Light";
    -fx-font-weight: 100;
    -fx-text-fill: white;
}

Directory: http://i.stack.imgur.com/c92ii.png

EDIT: System.out.println(font); now prints Font[name=StratumNo1-Thin, family=StratumNo1, style=Thin, size=10.0], so the file is being accessed correctly. However the font is still not being rendered on screen: http://i.stack.imgur.com/bueUk.png


回答1:


For the URL in Java code, try either

// relative to classpath, with leading /
Font font = Font.loadFont( Main.class.getClassLoader().getResourceAsStream( "/application/stratum.ttf"), 10);

or

// relative to class:
Font font = Font.loadFont( Main.class.getClassLoader().getResourceAsStream( "stratum.ttf"), 10);

The CSS looks right to me... are you sure your ttf file is being deployed along with the compiled code as css, etc?



来源:https://stackoverflow.com/questions/30245085/javafx-embed-custom-font-not-working

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