java 设置自定义字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13717481/
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
Setting custom font
提问by mangusbrother
I'm trying to set a custom font (bilboregular.ttf) to 2 jLabels in my program The fonts aren't being loaded successfully though.
我正在尝试在我的程序中将自定义字体 (bilboregular.ttf) 设置为 2 个 jLabel 但是字体没有成功加载。
Here is the main method calls:
下面是主要的方法调用:
//this should work if the build is in a jar file, otherwise it'll try to load it directly from the file path (i'm running in netbeans)
if (!setFonts("resources/bilboregular.ttf")) {
System.out.println("=================FAILED FIRST OPTION"); // <<<<<<<< This is being displayed
if(!setFonts(System.getProperty("user.dir")+"/src/resources/bilboregular.ttf")){
System.out.println("=================FAILED SECOND OPTION"); // <<< This is not being displayed
}
}
Here is the other method:
这是另一种方法:
public boolean setFonts(String s) {
try {
jLabel3.setFont(java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, new java.io.File(s)));
jLabel4.setFont(java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, new java.io.File(s)));
return true;
} catch (Exception ex) {
return false;
}
}
回答by Andrew Thompson
Firstly gain an URL
to the Font
. Then do something like this.
首先抢占URL
的Font
。然后做这样的事情。
'Airacobra Condensed' font available from Download Free Fonts.
'Airacobra Condensed' 字体可从下载免费字体 获得。
import java.awt.*;
import javax.swing.*;
import java.net.URL;
class LoadFont {
public static void main(String[] args) throws Exception {
// This font is < 35Kb.
URL fontUrl = new URL("http://www.webpagepublicity.com/" +
"free-fonts/a/Airacobra%20Condensed.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
JList fonts = new JList( ge.getAvailableFontFamilyNames() );
JOptionPane.showMessageDialog(null, new JScrollPane(fonts));
}
}
OK, that was fun, but what does this font actually look like?
好吧,这很有趣,但是这种字体实际上是什么样子的?
import java.awt.*;
import javax.swing.*;
import java.net.URL;
class DisplayFont {
public static void main(String[] args) throws Exception {
URL fontUrl = new URL("http://www.webpagepublicity.com/" +
"free-fonts/a/Airacobra%20Condensed.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
font = font.deriveFont(Font.PLAIN,20);
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
JLabel l = new JLabel(
"The quick brown fox jumped over the lazy dog. 0123456789");
l.setFont(font);
JOptionPane.showMessageDialog(null, l);
}
}
回答by mKorbel
- don't load a new
Font
repeatly, for each ofJLabel
separatelly
- 不要
Font
为每个JLabel
单独加载一个新的
means
方法
public boolean setFonts(String s) {
try {
jLabel3.setFont(java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, new java.io.File(s)));
jLabel4.setFont(java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, new java.io.File(s)));
return true;
} catch (Exception ex) {
return false;
}
}
- create a
Font
(s) asLocal variable
(s) and to change onlyjLabel3.setFont(myFont)
, or register a new Font (see link from @StanislavLs comment)
- 创建
Font
(s) asLocal variable
(s) and to change onlyjLabel3.setFont(myFont)
,或注册新字体(请参阅@StanislavLs 评论中的链接)
for example
例如
InputStream myFont = OptionsValues.class.getResourceAsStream(
"resources/bilboregular.ttf");