如何在Java Swing中使用Wingdings字体 [英] How to use Wingdings font in Java Swing

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

问题描述

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

When I try to use the Wingdings font (or other symbol fonts), the text comes out as rectangles instead of the correct text. How do I get the correct characters to show?

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);
    }
}


推荐答案

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

You need to use the appropriate Unicode range for the symbols you seek. In Java, symbols are not overlaid on the ASCII range, but have their own distinct character codes.

您可以在 http://unicode.org/~asmus/web-wing-ding-ext.pdf 。最常见的符号位于0x2200和0x2700 Unicode范围内。

You can find a reference to the appropriate symbol codes at http://unicode.org/~asmus/web-wing-ding-ext.pdf . Most common symbols are in the 0x2200 and 0x2700 Unicode ranges.

您的Java安装可能包含 SymbolTest 示例小程序,​​这样可以直接预览使用可用字体呈现Unicode范围。但是,请注意,更好的Java实现将对不在指定字体中的符号或字符使用字体替换,因此您需要确保实际获得指定的字体。

Your Java install may include the SymbolTest sample applet, which makes it straightforward to preview the presentation of Unicode ranges with available fonts. Be warned, however, that better Java implementations will use font substitutions for symbols or characters not in the specified font, so you'll want to be sure you're actually getting the specified font.

这篇关于如何在Java Swing中使用Wingdings字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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