JavaFX嵌入自定义字体不起作用 [英] JavaFX Embed Custom Font Not Working
问题描述
我正在使用JavaFX版本8.0.40-b27并尝试通过CSS嵌入自定义/外部字体。我也尝试过编程方法,所有方法都失败了。 System.out.print的font返回null,我怀疑这是原因。
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;
}
目录:
http://i.stack.imgur.com/c92ii .png
编辑:
System.out.println(font); 现在打印字体[name = StratumNo1-Thin,family = StratumNo1,style = Thin,size = 10.0] ,因此正确访问该文件。但是,字体仍未在屏幕上呈现: http://i.stack.imgur.com/ bueUk.png
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
推荐答案
对于Java代码中的URL,请尝试
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);
或
// relative to class:
Font font = Font.loadFont( Main.class.getClassLoader().getResourceAsStream( "stratum.ttf"), 10);
CSS看起来对我来说......你确定你的ttf文件正在与...一起部署编译代码为css等?
The CSS looks right to me... are you sure your ttf file is being deployed along with the compiled code as css, etc?
这篇关于JavaFX嵌入自定义字体不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!