简体   繁体   English

使用JavaFX SceneBuilder的自定义字体

[英]Custom Font with JavaFX SceneBuilder

JavaFX's SceneBuilder is very useful, but I want to add a custom font to it. JavaFX的SceneBuilder非常有用,但是我想为其添加自定义字体。 In the past, this has worked, though I have no idea how. 过去,这种做法行之有效,尽管我不知道如何做。 Screenshot: 截图:

在此处输入图片说明

Minecraftia is not in SceneBuilder as a default font, but it worked when I defined the Minecraftia font in CSS. Minecraftia不在SceneBuilder中作为默认字体,但是当我在CSS中定义Minecraftia字体时,它可以工作。 I tried doing the same with the new font i want (Roboto), but it doesn't show up in SceneBuilder. 我尝试用我想要的新字体(Roboto)进行同样的操作,但是它没有出现在SceneBuilder中。 I know that i could style the labels in CSS, but I liked it better when the font popped up in SceneBuilder. 我知道我可以在CSS中设置标签样式,但是当SceneBuilder中弹出字体时,我会更喜欢它。 Does anyone know why/how custom fonts show up in SceneBuilder? 有谁知道为什么/如何在SceneBuilder中显示自定义字体?

updated 更新

Since Java 8 JavaFX supports the @font-face rule that can be used to add fonts. 由于Java 8 JavaFX支持@ font-face规则,可用于添加字体。 As a first step the font file must be added to the application. 第一步,必须将字体文件添加到应用程序中。

Once this is done the font can be defined in CSS by using the @font-face rule: in your case 完成此操作后,可以使用@ font-face规则在CSS中定义字体

@font-face {
    font-family: 'Roboto';
    src: url('Roboto-Medium.ttf');
}

.text {
    -fx-font-family: "Roboto";
}

check this blog and a answer for more information. 查看此博客答案以获取更多信息。

Install the font to your system 将字体安装到您的系统

In windows 7 you can just double click the true type font file and it will ask if you want to install it... 在Windows 7中,您可以双击真正的字体文件,它会询问您是否要安装它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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