JButtonがあり、それにアイコンを追加したいと思います。TrueTypeフォントファイルを提供するFontAwesomeのフォントベースのアイコンを使用したいと思います。追加しようとしているアイコンは、再生ボタンのアイコンです。FontAwesomeのcssファイルにある再生ボタンアイコンは、Javaで\f04b
変換されると私は信じています。\uf04b
これは、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();
}
}
}
実装クラスStartButtonでは、これがテキストの設定方法です。
public class StartButton extends IconButton {
public StartButton() {
setText(String.valueOf('\u0f4b'));
setForeground(Color.BLACK);
}
}
これは私が得るものです。何もない。
どんな助けでも大歓迎です。
編集:以下の回答を参照してください。