获取系统默认字体

Tap*_*ose 12 java fonts

有没有办法在Java中获取系统默认字体名称?默认字体可能与操作系统不同.因此,如果我们使用字体Arial并且jar在Linux中运行而没有安装Arial字体,则会造成麻烦.

小智 27

试试这个:

private final Font FONT = new JLabel().getFont();
Run Code Online (Sandbox Code Playgroud)


Sup*_*tar 11

JavaFX使这更容易:

import javafx.scene.text.Font;
Run Code Online (Sandbox Code Playgroud)

然后使用:

Font defaultFont = Font.getDefault();
Run Code Online (Sandbox Code Playgroud)

要么

// Where 14 is the font size
Font defaultFont = new Font(14);
Run Code Online (Sandbox Code Playgroud)

  • `.getDefault()` 为我返回 `System`,所以这对我来说并不是那么有帮助。有什么获取 /real/ 字体系列的技巧吗? (4认同)

And*_*son 6

使用定义的Font常量,如SERIF/ SANS_SERIF等.

  • 这不是问题所在 (5认同)

Tro*_*eph 5

我目前正在使用它来获取默认字体,虽然我宁愿不需要使用图形对象来获取它:

        private final Font getFont()
            {
                Graphics g = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB).getGraphics();
                Font font = new Font(g.getFont().toString(), 0, 12);
                g.dispose();

                return font;
            }
Run Code Online (Sandbox Code Playgroud)


Car*_*ere 5

在这里查看public static Font Decode(String str)。当解码方法接收空指针作为参数时,它返回“Dialog”字体,通常是系统字体。