I have a JButton and I want to add an icon to it. I would like to use the font based icons from FontAwesome which provides a TrueType font file. The icon I am trying to add is the play button icon. The play button icon in the css file for FontAwesome is \f04b which I beleive translates to \uf04b in Java.
This is how I am loading the font in the constructor of by IconButton base class.
public class IconButton extends JButton {
public IconButton() {
try {
InputStream in = this.getClass().getResourceAsStream("/fontawesome-webfont.ttf");
Font ttfBase = Font.createFont(Font.TRUETYPE_FONT, in);
Font ttfReal = ttfBase.deriveFont(Font.BOLD, 24);
setFont(ttfReal);
} catch (FontFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the implementing class StartButton this is how I am setting the text.
public class StartButton extends IconButton {
public StartButton() {
setText(String.valueOf('\u0f4b'));
setForeground(Color.BLACK);
}
}
This is what I get. Nothing.

Any help is much appreciated.
EDIT: See answer below.
解决方案
I think I solved this actually. The correct character is \uf04b not \u0f4b. Whoops! :)
This is working for me now.

博主在尝试将Font Awesome的播放按钮图标添加到Java的JButton上,通过加载TrueType字体文件并设置按钮文本为Unicode字符。然而,发现显示为空。问题出在使用了错误的Unicode字符码,正确的是''而不是'ཋ'。修正后,图标成功显示。
752

被折叠的 条评论
为什么被折叠?



