在 JButton 上显示 Unicode 字符 [英] Display an Unicode character on JButton

查看:27
本文介绍了在 JButton 上显示 Unicode 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 JButton 文本上显示这个 Unicode "uD83D",但是当我编译它时,它只显示一个未知字符的正方形.>

I'm trying to display this Unicode "uD83D" on a JButton text, but when I compile it just shows the square of an unknown character.

推荐答案

Thomas 给出了很好的答案,但请注意,为了避免猜测哪些安装的字体支持字符或字符串,我们可以迭代可用字体并检查每个使用FontcanDisplayUpTo重载方法:

Thomas gave a good answer, but note that in order to avoid guessing which installed fonts support a character or string, we can iterate the available fonts and check each using the canDisplayUpTo overloaded methods of Font:

例如

import java.awt.Font;
import java.awt.GraphicsEnvironment;

public class FontCheck {

    public static void main(String[] args) {
        String s = "u4E33";
        Font[] fonts = GraphicsEnvironment.
                getLocalGraphicsEnvironment().getAllFonts();
        System.out.println("Total fonts: 	" + fonts.length);
        int count = 0;
        for (Font font : fonts) {
            if (font.canDisplayUpTo(s) < 0) {
                count++;
                System.out.println(font.getName());
            }
        }
        System.out.println("Compatible fonts: 	" + count);
    }
}

输出:

Total fonts:    391
Arial Unicode MS
Dialog.bold
Dialog.bolditalic
Dialog.italic
Dialog.plain
DialogInput.bold
DialogInput.bolditalic
DialogInput.italic
DialogInput.plain
Microsoft JhengHei
Microsoft JhengHei Bold
Microsoft JhengHei Light
Microsoft JhengHei UI
Microsoft JhengHei UI Bold
Microsoft JhengHei UI Light
Microsoft YaHei
Microsoft YaHei Bold
Microsoft YaHei Light
Microsoft YaHei UI
Microsoft YaHei UI Bold
Microsoft YaHei UI Light
Monospaced.bold
Monospaced.bolditalic
Monospaced.italic
Monospaced.plain
NSimSun
SansSerif.bold
SansSerif.bolditalic
SansSerif.italic
SansSerif.plain
Serif.bold
Serif.bolditalic
Serif.italic
Serif.plain
SimSun
Compatible fonts:   35

这篇关于在 JButton 上显示 Unicode 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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