Java SwingのAwesomeFontおよびUnicode文字からカスタムフォントを使用してJButtonにアイコンを追加しますか?

StackOverflow https://stackoverflow.com//questions/9639894

質問

JButtonを持っていて、私はそれにアイコンを追加したいです。TrueTypeフォントファイルを提供するFontAwesomeからフォントベースのアイコンを使用します。私が追加しようとしているアイコンは再生ボタンのアイコンです。FontAwesome用のCSSファイルのPlayボタンアイコンは\f04bです。これは、Javaの\uf04bに変換されます。

IconButton Baseクラスのコンストラクターにフォントをロードする方法です。

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();
    }
  }
}
.

実装クラスのスタートボタンでは、これがテキストの設定方法です。

public class StartButton extends IconButton {
  public StartButton() {
    setText(String.valueOf('\u0f4b'));
    setForeground(Color.BLACK);
  }
}
.

これは私が得るものです。何もありません。

jbutton

あらゆる助けが大いに評価されています。

編集:以下の回答を参照してください。

役に立ちましたか?

解決

私は実際にこれを解決したと思います。正しい文字は\uf04bではなく、\u0f4bです。who!:)

これは私のために働いています。

jbutton with Play Icon

他のヒント

JIConfont(SwingまたはJavaFX)で http://jiconfont.github.io/

例:

Icon icon = IconFontSwing.buildIcon(FontAwesome.FLOPPY_O, 15);

JButton button = new JButton(icon);
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top