Java – jlabel HTML text ignore SetFont

I've just started porting my swing application from OS X to windows, and jlabels is in pain

I noticed that if the text of the tag is HTML (which doesn't happen on MAC), the font assigned to SetFont will be ignored HTML format is very useful for readability of complex displays

Under normal circumstances, I will specify the font in the HTML tag, but the font I use will use font at runtime Createfont is loaded into ttf.xml in jar I tried to use the name of the loaded font in the font label, but it didn't work

Is there any way to use a loaded AWT Font and HTML verified jlabel on windows?

This is an example I can't share the font of my application, but I just run 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, if I use one of the TTFs in the Lib / fonts folder of JRE (in this case, a lucida font is renamed test_java. TTF here), this code segment will produce the same result as the Boolean value

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 of setting the default jlabel font described here is exactly the same problem (displayed in clear text, no HTML'd text): changing default jlabel font

Edit 3: I noticed that even if the random font of dafont is installed on the system (even for this exact code, I loaded a copy of [now installed] TTF from a file)

Solution

registerFont()

I found this little gem and Google about if I could run it at runtime Ttf copy to JRE It's really what it should be If you use font at runtime To load a font by createfont:

GraphicsEnvironment. getLocalGraphicsEnvironment(). registerFont(myCreatedFont)

Register JRE

In this way, the font and plaintext on windows can be displayed in HTML text!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>