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.
我有一个JButton,我想添加一个图标。我想使用FontAwesome中提供TrueType字体文件的基于字体的图标。我想添加的图标是播放按钮图标。 FontAwesome的css文件中的播放按钮图标是\ f04b,我在Java中将其转换为\ uf04b。
This is how I am loading the font in the constructor of by IconButton base class.
这就是我在IconButton基类的构造函数中加载字体的方法。
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.
在实现类StartButton中,这是我设置文本的方式。
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.
编辑:见下面的答案。
2 个解决方案
#1
12
I think I solved this actually. The correct character is \uf04b
not \u0f4b
. Whoops! :)
我想我实际上解决了这个问题。正确的字符是\ uf04b而不是\ u0f4b。哎呦! :)
This is working for me now.
这对我现在很有用。
#2
1
Try jIconFont (Swing or JavaFX) at http://jiconfont.github.io/
在http://jiconfont.github.io/上试试jIconFont(Swing或JavaFX)
Example:
例:
Icon icon = IconFontSwing.buildIcon(FontAwesome.FLOPPY_O, 15);
JButton button = new JButton(icon);
#1
12
I think I solved this actually. The correct character is \uf04b
not \u0f4b
. Whoops! :)
我想我实际上解决了这个问题。正确的字符是\ uf04b而不是\ u0f4b。哎呦! :)
This is working for me now.
这对我现在很有用。
#2
1
Try jIconFont (Swing or JavaFX) at http://jiconfont.github.io/
在http://jiconfont.github.io/上试试jIconFont(Swing或JavaFX)
Example:
例:
Icon icon = IconFontSwing.buildIcon(FontAwesome.FLOPPY_O, 15);
JButton button = new JButton(icon);