本文介绍了设置自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的程序中设置一个自定义字体(bilboregular.ttf)为2 jLabels字体没有被成功加载。
//这应该工作,如果构建在一个jar文件,否则它' (我在netbeans中运行)
if(!setFonts(resources / bilboregular.ttf)){
System.out.println(== =============== FAILED FIRST OPTION); //<<<<<<<<这是显示
if(!setFonts(System.getProperty(user.dir)+/ src / resources / bilboregular.ttf)){
System.out.println(== =============== FAILED SECOND OPTION); //<<<这是不显示
$ / code $ / pre
这是另一种方法:
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)));
返回true;
} catch(Exception ex){
return false;
$ / code $ / pre
解决方案获得一个 URL
到字体
。然后做这样的事情。
'Airacobra Condensed'font available from 。
import java.awt。*;
import javax.swing。*;
import java.net.URL;
$ b class LoadFont {
public static void main(String [] args)throws Exception {
//这个字体是< 35KB。
网址fontUrl =新网址(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));
好吧,那很有趣,但是这个字体实际上是什么样子?
import java.awt。*;
import javax.swing。*;
import java.net.URL;
$ b $ 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 =新的JLabel(
快速的棕色狐狸跳过懒惰的狗,0123456789);
l.setFont(font);
JOptionPane.showMessageDialog(null,l);
}
}
I'm trying to set a custom font (bilboregular.ttf) to 2 jLabels in my programThe fonts aren't being loaded successfully though.
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;
}
}
解决方案 Firstly gain an URL
to the Font
. Then do something like this.
'Airacobra Condensed' font available from Download Free Fonts.
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);
}
}
这篇关于设置自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!