JButton에 아이콘을 추가하십시오. Java Swing의 AwesomeFont 및 유니 코드 문자에서 사용자 지정 글꼴을 사용하여 자바 스윙?

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

문제

JButton이 있고 아이콘을 추가하고 싶습니다.트루 타입 글꼴 파일을 제공하는 Fontawesome에서 글꼴 기반 아이콘을 사용하고 싶습니다.내가 추가하려고하는 아이콘은 재생 단추 아이콘입니다.FontaweSome 용 CSS 파일의 재생 버튼 아이콘은 Java의 \f04b로 변호하는 \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();
    }
  }
}
.

구현 클래스 StartButton에서는 텍스트를 설정하는 방법입니다.

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

이것은 내가 얻는 것입니다.아무것도.

jbutton

도움이 많이 많이 감사합니다.

편집 : 아래 답변을 참조하십시오.

도움이 되었습니까?

해결책

I think I solved this actually. The correct character is \uf04b not \u0f4b. Whoops! :)

This is working for me now.

JButton with Play Icon

다른 팁

Try jIconFont (Swing or JavaFX) at http://jiconfont.github.io/

Example:

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

JButton button = new JButton(icon);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top