简体   繁体   English

为什么Font在不同的jres上加载不同?

[英]Why does Font load differently on different jres?

I am trying to load a font to a label/button text on a swing app written in java 1.4. 我试图将字体加载到用java 1.4编写的swing应用程序上的标签/按钮文本。 For some reason, the font loads without exception, but displays a completely diff font on the button. 出于某种原因,字体加载无例外,但在按钮上显示完全不同的字体。 When I run the same code on 1.5 jre, it appears to work ok. 当我在1.5 jre上运行相同的代码时,它似乎工作正常。 Why does this happen? 为什么会这样?

UPDATE Printing a sysout on 1.4, 1.5 shows this: 更新在1.4,1.5上打印sysout显示:

jre 1.5 : java.awt.Font[family=Futura LT,name=Futura LT Medium,style=bold,size=14] jre 1.5:java.awt.Font [family = Futura LT,name = Futura LT Medium,style = bold,size = 14]

jre 1.4 : java.awt.Font[family=dialog,name=Futura LT Medium,style=bold,size=14] jre 1.4:java.awt.Font [family = dialog,name = Futura LT Medium,style = bold,size = 14]

The font name family is different! 字体名称族不同! Why is that? 这是为什么?

Below is an image to how it looks on diff jres (left is 1.5, right is 1.4) 下面是它在diff jres上看起来的图像(左边是1.5,右边是1.4)

在此输入图像描述

Code

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JButton;
import javax.swing.SwingUtilities;

import org.jdesktop.swingx.MultiSplitLayout;
import org.jdesktop.swingx.MultiSplitPane;
import org.jdesktop.swingx.MultiSplitLayout.Node;



/**
 * 
 * @author Hans Muller (Hans.Muller@Sun.COM)
 */
public class Example2 extends Example {
    protected void initialize(String[] ignore) {
        super.initialize(ignore);

        String layoutDef = "(COLUMN (LEAF name=column1 weight=0.25) (LEAF name=column2 weight=0.25) (LEAF name=column3 weight=0.25) (LEAF name=column4 weight=0.25) )";
        Node modelRoot = MultiSplitLayout.parseModel(layoutDef);

        MultiSplitPane multiSplitPane = new MultiSplitPane();
        multiSplitPane.setDividerSize(5);
        multiSplitPane.getMultiSplitLayout().setModel(modelRoot);
        JButton comp = new JButton("TEST TEXT");
        comp.setFont(loadFont("FuturaLT.ttf",14,Font.BOLD));
        multiSplitPane.add(comp, "column1");
        multiSplitPane.add(new JButton("Test Text"), "column2");
        Container cp = mainFrame.getContentPane();
        cp.add(multiSplitPane, BorderLayout.CENTER);
    }

    private static Font loadFont(String fontName, float size, int style) {

        InputStream openStream = FontColorScheme.class
                .getResourceAsStream("resources/fonts/FuturaLT/"
                        + fontName);
        ;
        try {
            Font font = Font.createFont(Font.TRUETYPE_FONT, openStream);
            Font finalFont = font.deriveFont((float) size).deriveFont(style);
            System.out.println("Loading font " + fontName + " " + finalFont);
            return finalFont;
        } catch (FontFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (openStream != null) {
                try {
                    openStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static void main(final String[] args) {
        System.setProperty("awt.useSystemAAFontSettings", "off");
        Runnable doCreateAndShowGUI = new Runnable() {
            public void run() {
                try {
                    Example2 app = new Example2();
                    app.initialize(args);
                    app.show();
                } catch (Exception e) {
                    // TBD log an error
                }
            }
        };
        SwingUtilities.invokeLater(doCreateAndShowGUI);
    }
}

rendering is correct, there are three areas 渲染是正确的,有三个方面

This can have many reasons. 这可能有很多原因。 To understand this better, you should print the font that Java is using: 要更好地理解这一点,您应该打印Java正在使用的字体:

 System.out.println(button.getFont().toString());

That should answer the question whether both JREs use the same default font. 这应该回答两个JRE是否使用相同的默认字体的问题。 If they do, make sure the font is a TrueType font (TTF) and not a bitmap font; 如果是这样,请确保字体是TrueType字体(TTF)而不是位图字体; bitmap fonts don't scale well. 位图字体不能很好地扩展。 While TTF does scale better, some fonts look bad at certain sizes. 虽然TTF确实更好地扩展,但某些字体在某些尺寸上看起来很糟糕。 So a font might look well at 10 and 12 pixel but odd at 11. 所以字体可能看起来很好,10和12像素,但奇数在11。

To solve the issue, either set the font yourself, chose a different font if you already override the system default or try to tweak the font size. 要解决此问题,请自行设置字体,如果已覆盖系统默认值,请选择其他字体或尝试调整字体大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么TreeSet的add方法在不同的JRE中表现不同? - Why does TreeSet's add method behaves differently in different JREs? 为什么每个JDK都带有3个JRE? - Why does each JDK come with 3 JREs? 为什么我的文本规范化在不同环境中的行为会有所不同? - Why does my text normalization behave differently in different environments? 为什么递归在不同的递归调用位置上有不同的作用 - Why does recursion act differently for different placements of recursive call 为什么我的JButton在不同的计算机上看起来有所不同? - Why does my JButton look differently of different computers? 不同的 JRE 使用什么 substring 搜索算法? - What substring search algorithm is used by different JREs? 在不同的JRE上运行Java类文件的后果? - Consequences of running a Java Class file on different JREs? 为什么插入排序在不同的实现方式下在时间上有很大不同 - Why does insertion-sort run so much differently time-wise with different implementation 为什么java.lang.process的readline()在具有相同操作系统的不同框上读取输入流的行为不同 - Why does java.lang.process's readline() behave differently for reading inputstream on different boxes with the same os 为什么listFiles()在不同的平台上表现不同? - Why listFiles() behaves differently in different platform?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM