我刚刚开始将我的Swing应用程序从OS X移植到Windows,JLabel
s很痛苦。
我注意到,如果标签的文本是HTML(Mac上不会发生这种情况),则忽略指定给setFont
的字体。 HTML格式对于复杂显示的可读性非常有用。
在正常情况下,我会在HTML标记中指定字体,但我正在使用的字体是使用Font.createFont
在运行时加载的,其中ttf来自JAR。我尝试在字体标记中使用加载的字体名称,但这不起作用。
有什么方法可以在Windows上使用带有html-ified awt.Font
的加载JLabel
?
这是一个例子。我不能分享我的应用程序的字体,但我只是用这个(纯TTF)运行它,并发生相同的行为:
http://www.dafont.com/sophomore-yearbook.font
import java.awt.Font;
import java.io.File;
import javax.swing.*;
public class LabelTestFrame extends JFrame {
public LabelTestFrame() throws Exception {
boolean useHtml = true;
String fontPath = "C:\\test\\test_font.ttf";
JLabel testLabel = new JLabel();
Font testFont = Font.createFont(Font.TRUETYPE_FONT, new File(fontPath)).deriveFont(18f);
testLabel.setFont(testFont);
if (useHtml) testLabel.setText("<html>Some HTML'd text</html>");
else testLabel.setText("Some plaintext");
getContentPane().add(testLabel);
setSize(300,300);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {new LabelTestFrame().setVisible(true);}
catch (Exception e) {e.printStackTrace();}
}
});
}
}
编辑:有趣的是,如果我使用JRE的lib / fonts文件夹中的一个ttf(在这种情况下,其中一个Lucida字体重命名为test_java.ttf),这个片段会产生与boolean on和off相同的结果。
public LabelTestFrame() throws Exception {
boolean useHtml = false;
String fontPath = "C:\\test\\test_java.ttf";
JLabel testLabel = new JLabel();
Font testFont = Font.createFont(Font.TRUETYPE_FONT, new File(fontPath)).deriveFont(18f);
testLabel.setFont(testFont);
if (useHtml) testLabel.setText("<html><b>Some HTML'd text</b></html>");
else testLabel.setText("Some plaintext");
getContentPane().add(testLabel);
setSize(300,300);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {new LabelTestFrame().setVisible(true);}
catch (Exception e) {e.printStackTrace();}
}
});
}
编辑2:此处描述的用于设置默认JLabel字体的方法具有完全相同的问题(明文显示正常,html文本没有):Changing default JLabel font
编辑3:我注意到即使是dafont的随机字体也会在系统上安装(即使使用这个确切的代码,我从文件中加载了[now installed] ttf的副本) )。
答案 0 :(得分:12)
我在Google搜索时发现了这个小宝石,我是否可以在运行时将.ttf
复制到JRE中。它完全符合预期。如果使用Font.createFont
在运行时加载字体,只需执行:
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(myCreatedFont)
将其注册到JRE。
这允许字体显示在HTML文本以及Windows上的纯文本中!
答案 1 :(得分:4)
供参考,这是在Mac OS X上看到的内容。
相比之下,这是Ubuntu 10,OpenJDK 6上的显示。
import java.awt.Font;
import java.awt.GridLayout;
import java.io.File;
import javax.swing.*;
public class LabelTestFrame extends JFrame {
public LabelTestFrame() throws Exception {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(0, 1));
String fontPath = "SophomoreYearbook.ttf";
Font testFont = Font.createFont(
Font.TRUETYPE_FONT, new File(fontPath)).deriveFont(18f);
JLabel label1 = new JLabel("<html>Some HTML'd text</html>");
label1.setFont(testFont);
this.add(label1);
JLabel label2 = new JLabel("Some plaintext");
this.add(label2);
this.pack();
this.setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
new LabelTestFrame().setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}