简体   繁体   中英

JavaFX Embed Custom Font Not Working

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM