在JButton上显示一个Unicode字符 [英] Display an Unicode character on JButton
问题描述
我试图在 JButton
文本上显示这个Unicode \\\�
,但是当我编译它只是显示一个未知字符的平方。
你需要设置一个支持你想要的Unicode字符的字体。
以下示例依赖于
(在D800-DFFF的范围内),您需要使用高和低代理对。
请注意,这个对代表的是一个超过 \\\
的Unicode点。
代理对表示代码点
10000 16 +(H-D800 <16>)×400 16 > +(L - DC00 16 )
其中H和L分别是高位和低位代理的数值。
字符串中的未配对代理字符(如原始问题中)
是无效的,并将呈现为。
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.
You need to set a font supporting the Unicode characters you want.
The following example relies on Code2000.ttf
installed on my system.
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("\u4E33");
Font font = new Font("Code2000", Font.PLAIN, 36);
button.setFont(font);
frame.add(button);
frame.pack();
frame.setVisible(true);
});
}
When you use surrogate characters
(in range D800–DFFF), you need to use a high and low surrogate pair.
And be aware that this pair represents a Unicode point beyond \uFFFF
.
A surrogate pair denotes the code point
1000016 + (H − D80016) × 40016 + (L − DC0016)
where H and L are the numeric values of the high and low surrogates respectively.
An unpaired surrogate character in a string (as in the original question) is invalid, and will be rendered as .
这篇关于在JButton上显示一个Unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!