Java Swing 字体真棒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24177348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Font Awesome with Swing
提问by Akshay Raut
Is it possible to use Font Awesome with swing applications? If possible then how to use its icons with swing components (JButton or JLabel). I've used Font Awesome earlier with my Primefaces application.
是否可以将 Font Awesome 与 Swing 应用程序一起使用?如果可能,那么如何将其图标与摆动组件(JButton 或 JLabel)一起使用。我之前在 Primefaces 应用程序中使用过 Font Awesome。
采纳答案by Carlos Eduardo
Try jIconFont (Swing or JavaFX) at http://jiconfont.github.io/
在http://jiconfont.github.io/尝试 jIconFont(Swing 或 JavaFX)
Example:
例子:
Icon icon = IconFontSwing.buildIcon(FontAwesome.SMILE_O, 40, new Color(0, 150, 0));
JLabel label = new JLabel(icon);
回答by MadProgrammer
I would say "yes"...
我会说“是”...
- Download the zip package from Font Awesome
- Uncompress it
- Copy the
fontawesome-webfont.ttf
file to your project (in the below example, I used it as an embedded resource) - Using the Cheeatsheet, copy and past the icon you want to use into your code
- Load the font and display...
- 从Font Awesome下载 zip 包
- 解压
- 将
fontawesome-webfont.ttf
文件复制到您的项目中(在下面的示例中,我将其用作嵌入式资源) - 使用Cheeatsheet,将要使用的图标复制并粘贴到代码中
- 加载字体并显示...
For example...
例如...
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();
}
}
});
}
}
You can also use the unicode directly, for example, the symbol in the above example is listed as 
which could be used as...
也可以直接使用unicode,例如上面例子中的符号被列为
可以用作...
JLabel label = new JLabel("\uf0c0");