gpt4 book ai didi

java - 为什么字体在不同的 jres 上加载不同?

转载 作者:行者123 更新时间:2023-11-29 05:42:21 26 4
gpt4 key购买 nike

我正在尝试将字体加载到用 java 1.4 编写的 swing 应用程序上的标签/按钮文本。由于某种原因,字体加载无一异常(exception),但在按钮上显示完全不同的字体。当我在 1.5 jre 上运行相同的代码时,它似乎工作正常。为什么会这样?

更新在 1.4、1.5 上打印系统输出显示:

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]

字体名族不同!这是为什么?

下图是它在 diff jres 上的样子(左边是 1.5,右边是 1.4)

enter image description here

代码

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);
}
}

最佳答案

渲染正确,有3个区域

关于java - 为什么字体在不同的 jres 上加载不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17059967/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com