【问题标题】:Font Awsome on JButtonJButton 上的字体真棒
【发布时间】:2014-10-07 10:35:52
【问题描述】:

我正在尝试在 JButton 上显示 font awesome 图标。我做了以下

Font font = null;
try
{
    font = Font.createFont(Font.TRUETYPE_FONT, new File("../icons/fontawesome-webfont.ttf"));
    font = font.deriveFont(18f);

    GraphicsEnvironment ge = 
        GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.registerFont(font);
}
catch ( FontFormatException ex ){ex.printStackTrace();}
catch ( IOException ex ){ex.printStackTrace();}

private JButton save_btn = new JButton();
save_btn.setFont(font);
save_btn.setText("\uf0c7");
save_btn.setPreferredSize( buttonDimension );

但我得到的都是

如果我使用带有 JTextArea 的字体,它可以正常工作

JTextArea t = new JTextArea();
t.setRows( 2);
t.setColumns( 12);
t.setFont( font );
t.setText( "\uF0F3 \uF1EC \uf0f3 \uf1ec");
buttonPanel.add(t);

我收到

【问题讨论】:

  • System.out.println(font.canDisplay("\uf0c7")); 的输出是什么?
  • 检查天气系统不支持字体吗?
  • 您引用了正确的字符代码吗?这是一个类似的问题:stackoverflow.com/questions/9639894/…
  • ge.registerFont(字体);返回一个布尔值。它的价值是什么?
  • font.canDisplay && registerFont 都返回 true。

标签: java swing


【解决方案1】:

我的猜测是您的按钮尺寸太小,无法显示您要显示的符号,并且按钮恢复为显示“...”,这似乎没有很好的字体表示。

【讨论】:

【解决方案2】:

我发现了问题,我删除了

save_btn.setPreferredSize( buttonDimension );

现在它可以正常工作了!

【讨论】:

  • 您也可以将边距插入设置为 0。这样左右边距就会进入(如果这是您尝试对 setPreferredSize 执行的操作... button.setMargins(new Insets(0, 0, 0, 0));
猜你喜欢
  • 2020-09-24
  • 2020-06-16
  • 2014-08-02
  • 2017-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-23
相关资源
最近更新 更多