swing创建字体

Yeg*_*xim 5 java fonts swing

我有一些自定义组件与overriden paintComponent(Graphics g)方法.在一些组件中drawString(),Graphics g使用来自对象的方法.我想为这种绘制的字符串定义自定义字体.

我有*.ttf文件与真正的字体定义.我有一个带有样式类的css文件,定义如下字体:

.rosTexLogoTitle {
   -fx-font: bold 20pt 'Tahoma Bold';
   -fx-text-fill: #246db6;
   -fx-font-weight: heavybold;
   -fx-padding: 0 10 0 0
Run Code Online (Sandbox Code Playgroud)

我想在我的java类中定义css的所有属性,或者有一种方法可以直接从css文件中使用样式(不是必需的).

我试图用带有Map参数的构造函数创建字体:

public Font(Map<? extends Attribute, ?> attributes) {
}
Run Code Online (Sandbox Code Playgroud)

但我没有成功.我可以创建自定义字体Font.createFont(style, path)然后可以派生它来设置大小.颜色可以设置Graphics.setColor().但我不能设置'Tahoma Bold'.

Aar*_*lla 2

您无法设置字体粗细或字体家族;这些仅用于选择正确的.ttf文件。

.ttf因此,解决方案是首先使用正确的文件。例如,字体系列Arial有四个文件:

arialbd.ttf - Weight "Bold"
arialbi.ttf - Weight "Bold", Slant "Italic"
ariali.ttf  - Slant "Italic"
arial.ttf   - Plain version
Run Code Online (Sandbox Code Playgroud)