繁体   English   中英

某些FontAwesome字形不会在Java Swing JToolBar按钮中呈现

[英]Certain FontAwesome glyphs don't render in Java Swing JToolBar buttons

我在Swing JToolBar中的按钮中显示来自FontAwesome集合的某些字形时遇到问题。 这是一个截图(注意右侧工具栏中的顶部按钮不是一个漂亮的图标,而是显示三个空矩形):

屏幕截图来说明问题

重现这个的代码(至少在我的Mac上)是:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;![enter image description here][2]
import java.awt.FontFormatException;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;

public class TestFontAwesome {

    public static void main(String[] args) {
        new TestFontAwesome();
    }

    public TestFontAwesome() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try (InputStream is = TestFontAwesome.class.getResourceAsStream("/fontawesome-webfont_old.ttf")) {
                    Font font = Font.createFont(Font.TRUETYPE_FONT, is);
                    font = font.deriveFont(Font.PLAIN, 24f);

                    JToolBar toolBar = new JToolBar(JToolBar.VERTICAL);
                    JButton button1 = new JButton("\uf00e");
                    button1.setFont(font);
                    toolBar.add(button1);
                    JButton button2 = new JButton("\uf01e");
                    button2.setFont(font);
                    toolBar.add(button2);
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new JButton("Irrelevant content..."));
                    frame.add(toolBar, BorderLayout.EAST);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException | FontFormatException exp) {
                    exp.printStackTrace();
                }
            }
        });
    }

}

我尝试了一些方法:(1)使用不同版本的FontAwesome.ttf文件,没有变化; (2)尝试不同的JDK版本,没有变化; (3)在常规JButton中显示相同的字符,这可以在下面的屏幕截图中看到(所以这显然不是字体文件的一些问题):

屏幕截图显示它适用于常规JButton

我在非Retina Mac上进行了测试,一切正常,所以我想知道这是否是Retina显示器特有的。 如果有人有任何建议,我将不胜感激,谢谢。

仅JButton示例的代码(运行正常)是:

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JButton;
import javax.swing.JFrame;

public class TestFontAwesome2 {

    public static void main(String[] args) {
        new TestFontAwesome2();
    }

    public TestFontAwesome2() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try (InputStream is = TestFontAwesome.class.getResourceAsStream("/fontawesome-webfont_old.ttf")) {
                    Font font = Font.createFont(Font.TRUETYPE_FONT, is);
                    font = font.deriveFont(Font.PLAIN, 24f);

                    JButton button1 = new JButton("\uf00e");
                    button1.setFont(font);
                    JButton button2 = new JButton("\uf01e");
                    button2.setFont(font);
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new FlowLayout());
                    frame.add(new JButton("Irrelevant content..."));
                    frame.add(button1);
                    frame.add(button2);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException | FontFormatException exp) {
                    exp.printStackTrace();
                }
            }
        });
    }

}

我认为问题是ComponentUi
特殊方法:ToolbarUi或ButtonUi(-Implementation)。

ToolbarUi(和ButtonUi)是抽象类,在您选择的LookAndFeel中实现。
每个LookAndFeel的实现可能完全不同。
某些实现会忽略某些“用户”设置,例如字体颜色

JButtons可以使用与添加到JToolBars的按钮不同的Ui实现!
此实现可能会忽略您的字体设置。

见在MetalLookAndFeel例如ButtonUi执行( 仅限部分

public void update(Graphics g, JComponent c) {
   AbstractButton button = (AbstractButton)c;
   if ((c.getBackground() instanceof UIResource) &&
             button.isContentAreaFilled() && c.isEnabled()) {
       ButtonModel model = button.getModel();
       if (!MetalUtils.isToolBarButton(c)) {
           if (!model.isArmed() && !model.isPressed() &&
                   MetalUtils.drawGradient(
                   c, g, "Button.gradient", 0, 0, c.getWidth(),
                   c.getHeight(), true)) {
               paint(g, c);
               return;
           }
       }
...

在这里,您可以看到MetalUtils.isToolbarButton时的不同行为

您必须检查LookAndFeel实现行为。
(也许还有不同的实现,具体取决于屏幕分辨率)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM