使用AwesomeFont的自定义字体和Java Swing中的unicode字符将图标添加到JButton? [英] Add icons to JButton using custom font from AwesomeFont and unicode characters in Java Swing?
问题描述
我有一个JButton,我想给它添加一个图标。我想使用FontAwesome提供的TrueType字体文件的字体图标。我试图添加的图标是播放按钮图标。 FontAwesome的css文件中的播放按钮图标是 \f04b
,我相信在Java中转换为 \\\
。
这就是我在IconButton基类的构造函数中加载字体的方法。
public class IconButton继承JButton {
public IconButton(){
try {
InputStream in = this.getClass()。getResourceAsStream(/ fontawesome-webfont.ttf);
字体ttfBase = Font.createFont(Font.TRUETYPE_FONT,in);
字体ttfReal = ttfBase.deriveFont(Font.BOLD,24);
setFont(ttfReal);
} catch(FontFormatException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
$ p $在实现类StartButton中,
public class StartButton extends IconButton {
public StartButton(){
setText(String.valueOf('\\\ཋ'));
setForeground(Color.BLACK);
$ b这是我得到的。没有什么。
任何帮助都非常感谢。
编辑:请参阅下面的答案。
解决方案我想我已经解决了这个问题。正确的字符是 \\\
不是 \\\ཋ
。哎呦! :)
现在这个功能对我来说很有用。
$ b
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.
这篇关于使用AwesomeFont的自定义字体和Java Swing中的unicode字符将图标添加到JButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!