JavaFX Embed Custom Font Not Working

2019-06-06 05:06发布

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条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-06-06 05:20

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?

查看更多
登录 后发表回答