如何使用CSS在JavaFX Scene Builder中设置自定义字体 [英] How to set custom fonts in JavaFX Scene Builder using CSS

查看:4549
本文介绍了如何使用CSS在JavaFX Scene Builder中设置自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaFX场景生成器中创建一个GUI,并希望所有文本(标签,文本字段,组合框)使用相同的字体。问题是它是一个自定义字体,可能不会在每个客户端的计算机上。

I'm making a GUI in JavaFX Scene Builder and would like all text (Labels, Text Fields, Comboboxes) to use the same font. The problem is it's a custom font that likely won't be on every client's computer.

我尝试过CSS:

@font-face {
   font-family: DIN;
   src: url(DIN.tff);
}

.text {
   -fx-font-family: DIN;
   -fx-font-style: bold;
}

保存上述代码到 Font.css 并尝试通过Scene Builder的 JavaFX CSS 字段将其应用到每个GUI元素,但它不工作。

Saved the above code to file Font.css and tried applying it to each GUI element through Scene Builder's JavaFX CSS fields, but it's not working.

我在CSS中犯了一个错误?还是有更好的办法去做这个?我想不必加载字体,并为Java代码本身中的每个元素设置它,如果我不必。

Have I made a mistake in the CSS? Or is there a better way to go about this? I'd prefer not to have to load the font and set it for every element in the Java code itself if I don't have to.

推荐答案

确保使用 Font.loadFont 在应用程序启动时实际加载字体。然后你应该能够使用CSS的字体。请小心使用字体的名称,而不是字体文件的名称。这是一个常见的错误。

Make sure to use Font.loadFont to actually load the font on application startup. Then you should be able to use the font from CSS. Be careful to use the font's name, not the font file's name. That's a common mistake.

我使用了以下加载和使用自定义字体:

I have used the following before to load and use a custom font:

Font.loadFont(getClass().getResourceAsStream("/resources/fonts/marck.ttf"), 14);

和:

-fx-font-family: "Marck Script";

FYI:只有在名称包含空格时才需要引号。

FYI: the quotes are only needed if the name contains a space.

这篇关于如何使用CSS在JavaFX Scene Builder中设置自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆