如何在 Java Swing GUI 中设置字母间距?

How to set letter spacing in Java Swing GUI?

在CSS中,有一个字母间距 属性,我们可以用它来增加单词中每个字母的间距。

有什么方法可以增加字母 space 或文本 space 吗?我希望我可以增加字段中文本的 space。

这是一个例子:

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        JLabel label = new JLabel("This is a text.");
        Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>();
        attributes.put(TextAttribute.TRACKING, 0.5);

        label.setFont(label.getFont().deriveFont(attributes));
        JOptionPane.showMessageDialog(null, label);
    });
}

结果: