4

我有一个在 Windows XP 中开发的 Java 7 Swing 应用程序。我使用跨平台外观:

UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

当我在 linux (Fedora 12) 上运行它时。字体有点宽。因此,布局在某些地方发生了变化。

有没有办法让应用程序使用 jre 附带的相同字体或类似字体,但不是本地系统?然后我们可以让它的外观和感觉真的一样。

谢谢,

4

3 回答 3

4

你可以看看UIManager。Swing 使用此处的属性,例如Label.font,来获取默认字体。所以你可以这样做:

Font font = // create font
UIManager.put("Label.font", font)

确保在创建任何组件之前更改这些内容,否则您将获得一些具有正确字体的字体,而另一些则没有。这是一个程序,它将向您显示UIManager. 任何以结尾的.font都是你要找的。

另一种方法是创建一个实用程序类,该类将使用您自己的默认值创建组件:

public class MyComponents {

    public static final Font LABEL_FONT = // create font

    public static JLabel createLabel(String text) {
        JLabel label = new JLabel(text);
        label.setFont(LABEL_FONT);
        return label;
    }
}

如果这是一个没有很多组件的新应用程序,我推荐第二种方法。如果它是一个旧的应用程序,到处都有大量的组件生成,第一种方法会更省时,但第二种方法可能会更好。

于 2012-08-30T18:15:33.730 回答
1

This may not work, but. You could provide your own Font, bundled with the application and load that at Runtime.

Check out Load fonts out of JAR-file and create AWT font (works) and register iText font (does not work) for an example (the answers not quite right, but the intention is good) or http://www.java2s.com/Code/Java/2D-Graphics-GUI/Loadfontfromttffile.htm

Once loaded, you could walk the UIManager's properties, replacing all keys with ".font" in there names to your Font instead.

The problem you are going to face ultimately is the difference in the rendering engines of the OS, including things like DPI and the difference in the Font rendering engines.

于 2012-08-30T21:34:32.627 回答
1

目前尚不清楚这是字体问题还是布局问题。自然,sscce将有助于说明您认为是什么问题。相比之下,此示例说明了如何调整布局以适应不同平台上的默认字体系列。

当您使用跨平台 L&F时,您可能需要关闭粗体字体,如此处所示。一些平台没有使用实际的粗体字体,而是从普通字体中派生出粗体样式,通常结果参差不齐。

于 2012-08-31T15:19:42.060 回答