在 JavaFX CSS 中指定外部字体 [英] Specifying external font in JavaFX CSS
本文介绍了在 JavaFX CSS 中指定外部字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在 JavaFX 应用程序中使用 CSS 指定字体?我有一个 FXML 场景和相应的 CSS 文件.在 Java 代码中,可以使用 setFont
方法指定字体:
Is it possible to specify a font using CSS in JavaFX application? I have an FXML scene and the corresponding CSS file. In Java code, it's possible to specify a font using the setFont
method:
text.setFont(Font.loadFont("file:resources/fonts/SourceSansPro-Bold.ttf", 12));
我尝试了不同的结构,例如:
I tried different constructions, like:
-fx-font-family: url("../fonts/SourceSansPro-Bold.ttf");
但它不起作用.
推荐答案
您已经接近解决方案了:
You were close to the solution:
-fx-font-family: 'Helvetica', Arial, sans-serif;
试试这个,应该没问题.
Try this, it should work fine.
也许这会有所帮助?(我不是 JavaFX 专家)
Maybe this could help? (I'm not an expert in JavaFX)
https://forums.oracle.com/forums/thread.jspa?messageID=10107625
编辑:
加载您的字体:
@font-face {
font-family: Delicious;
src: url('Delicious-Roman.otf');
}
使用带有 -fx-
的字体:
-fx-font-family: 'Delicious';
这篇关于在 JavaFX CSS 中指定外部字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文