问题
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