4

当我尝试使用 Wingdings 字体(或其他符号字体)时,文本显示为矩形而不是正确的文本。如何让正确的字符显示?

import java.awt.*;
import javax.swing.*;

public class WingdingsFontDisplay extends JFrame
{
    public static void main(String[] args)
    {
        new WingdingsFontDisplay();
    }

    public WingdingsFontDisplay()
    {
        this.setSize(500,150);
        this.setTitle("Fun with Fonts");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //This shows that I do have "Wingdings" available
        GraphicsEnvironment g;
        g = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fonts = g.getAvailableFontFamilyNames();
        for(String f : fonts)
        {
            System.out.println(f);
        }

        //Displaying text in the Wingdings font shows rectangles
        JLabel wingdingsText = new JLabel("All work and no play makes Jack a dull boy");
        Font f1 = new Font("Wingdings", Font.PLAIN, 14);
        wingdingsText.setFont(f1);
        this.add(wingdingsText, BorderLayout.NORTH);

        //Displaying text in Arial works correctly
        JLabel arialText = new JLabel("All work and no play makes Jack a dull boy");
        Font f2 = new Font("Arial", Font.PLAIN, 14);
        arialText.setFont(f2);
        this.add(arialText, BorderLayout.SOUTH);

        this.setVisible(true);
    }
}
4

1 回答 1

5

您需要为您寻找的符号使用适当的 Unicode 范围。在 Java 中,符号不会覆盖在 ASCII 范围内,而是有自己独特的字符代码。

您可以在http://unicode.org/~asmus/web-wing-ding-ext.pdf找到对相应符号代码的引用。最常见的符号在 0x2200 和 0x2700 Unicode 范围内。

您的 Java 安装可能包括SymbolTest示例小程序,​​它可以直接预览 Unicode 范围与可用字体的表示。但是请注意,更好的 Java 实现将使用字体替换符号或不采用指定字体的字符,因此您需要确保您确实获得了指定的字体。

于 2012-10-06T05:03:05.460 回答