Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

java - Display an Unicode character on JButton

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

E.G.

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

Output:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...