字体真棒与秋千

Aks*_*aut 13 java fonts swing font-awesome

是否可以将Font Awesome与swing应用程序一起使用?如果可能,那么如何将其图标与swing组件(JButton或JLabel)一起使用.我之前在Primefaces应用程序中使用过Font Awesome.

Mad*_*mer 23

我会说"是"......

在此输入图像描述

  • Font Awesome下载zip包
  • 解压缩它
  • fontawesome-webfont.ttf文件复制到项目中(在下面的示例中,我将其用作嵌入式资源)
  • 使用Cheeatsheet,将要使用的图标复制并粘贴到代码中
  • 加载字体和显示...

例如...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GridBagLayout;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestFontAwsome {

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

    public TestFontAwsome() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                try (InputStream is = TestFontAwsome.class.getResourceAsStream("/fontawesome-webfont.ttf")) {
                    Font font = Font.createFont(Font.TRUETYPE_FONT, is);
                    font = font.deriveFont(Font.PLAIN, 24f);

                    JLabel label = new JLabel("?");
                    label.setFont(font);

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new GridBagLayout());
                    frame.add(label);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException | FontFormatException exp) {
                    exp.printStackTrace();
                }
            }
        });
    }

}
Run Code Online (Sandbox Code Playgroud)

您也可以直接使用unicode,例如,列出上面示例中的符号,可以将其用作...

 JLabel label = new JLabel("\uf0c0");
Run Code Online (Sandbox Code Playgroud)


小智 9

http://jiconfont.github.io/上试试jIconFont(Swing或JavaFX)

例:

Icon icon = IconFontSwing.buildIcon(FontAwesome.SMILE_O, 40, new Color(0, 150, 0));

JLabel label = new JLabel(icon);
Run Code Online (Sandbox Code Playgroud)