JLabel html文本忽略setFont [英] JLabel html text ignores setFont
问题描述
我刚刚开始将我的Swing应用程序从OS X移植到Windows,而使用 JLabel
s的事情是痛苦的。
我注意到,如果标签的文本是HTML(Mac上不会发生这种情况),指定给 setFont
的字体将被忽略。 HTML格式对复杂显示的可读性是非常有用的。
在正常情况下,我会在HTML标签中指定字体,但是我使用的字体已经加载在运行时使用 Font.createFont
和一个ttf输出JAR。我尝试在字体标签中使用加载的字体的名称,但没有奏效。
有什么办法可以使用加载的 awt .font
在Windows上使用html-ified 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 {
$ b $ public LabelTestFrame()throws Exception {
boolean useHtml = true;
String fontPath =C:\\test\\test_font.ttf;
JLabel testLabel = new JLabel();
字体testFont = Font.createFont(Font.TRUETYPE_FONT,new File(fontPath))。deriveFont(18f);
testLabel.setFont(testFont); (useHtml)testLabel.setText(< html>一些HTML文本< / 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);} $ b $ catch(Exception e){e.printStackTrace();}
}
});
$ b 编辑:有趣的是,如果我使用JRE的lib / fonts文件夹中的一个ttf(在这种情况下,其中一个Lucida字体在这里重命名为test_java.ttf),这个片断会产生和布尔型相同的结果。
public LabelTestFrame()抛出异常{
boolean useHtml = false;
String fontPath =C:\\test\\test_java.ttf;
JLabel testLabel = new JLabel();
字体testFont = Font.createFont(Font.TRUETYPE_FONT,new File(fontPath))。deriveFont(18f);
testLabel.setFont(testFont);
if(useHtml)testLabel.setText(< html>< b>一些HTML文本< / 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);} $ b $ catch(Exception e){e.printStackTrace();}
}
});编辑2:这里描述的设置默认的JLabel字体的方法正好是同样的问题(明文显示罚款,html'd文本没有):更改默认的JLabel字体编辑3:我注意到,甚至从dafont随机字体将工作,如果他们安装在系统上(即使有这个确切的代码,我在哪里加载从文件中获得[now installed] ttf的副本)。 oracle.com/javase/6/docs/api/java/awt/GraphicsEnvironment.html#registerFont%28java.awt.Font%29rel =noreferrer> registerFont()
我在搜索时发现了这个小宝石,可以将 .ttf
复制到运行时的JRE。它确实是它应该的。如果你使用 Font.createFont
在运行时加载字体,只需要:
GraphicsEnvironment.getLocalGraphicsEnvironment()。registerFont(myCreatedFont)
使用JRE注册。
这允许字体显示在HTML文本中以及Windows上的明文!
I've just started porting my Swing app from OS X to Windows and things are painful with JLabel
s.
I've noticed that the font specified to setFont
is ignored if the label's text is HTML (this doesn't happen on the Mac). The HTML formatting is EXTREMELY useful for readability on complicated displays.
Under normal circumstances I'd specify the font in an HTML tag, but the font I'm using is loaded at runtime using Font.createFont
with a ttf out of the JAR. I tried using the loaded font's name in the font tag, but that didn't work.
Is there any way I can use a loaded awt.Font
with an html-ified JLabel
on Windows?
Here's an example. I can't share my application's font, but I just ran it with this one (a pure TTF) and the same behavior happens:
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();}
}
});
}
}
EDIT: interestingly enough, if I use one of the ttf's from the JRE's lib/fonts folder (in this case one of the Lucida fonts here renamed to test_java.ttf) this snippet produces identical results with the boolean on and 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();}
}
});
}
EDIT 2: The method described here for setting the default JLabel font has exactly the same problem (plaintext shows fine, html'd text doesn't): Changing default JLabel font
EDIT 3: I've noticed that even random fonts from dafont will work if they're installed on the system (even with this exact code, where I'm loaded a copy of the [now installed] ttf from a file).
解决方案
I found this little gem while Googling about if I could copy a .ttf
into the JRE at runtime. It does exactly what it's supposed to. If you use Font.createFont
to load a font at runtime, just do:
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(myCreatedFont)
to register it with the JRE.
This allows the font to show up in HTML'd text as well as plaintext on Windows!
这篇关于JLabel html文本忽略setFont的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!