本文介绍了Java/Swing字体调整大小在跨平台上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有以下代码的swing应用程序,该应用程序试图调整组件字体的大小:

  Font f = new Font(Font.SERIF,Font.PLAIN,16);component.setFont(f); 

程序加载14磅字体,并且我包含一个组件,该组件使用户可以如上所述将组件的大小调整为16磅字体.当我在(Debian)上的计算机上运行它时,它可以按预期工作,但是我在Mac和Windows计算机上对其进行了测试,并且字体大小调整功能根本不起作用.但是,在所有平台上,字体均为衬线.任何想法为什么会发生这种情况?

解决方案

您平台上组件的默认UI委托可能会抢占您的设置.或者,请考虑以下之一:

  • 在现有字体上使用 deriveFont(),如和.

I have a swing application with this code, which attempts to resize the font of a component:

Font f = new Font(Font.SERIF,Font.PLAIN,16);
component.setFont(f);

The program loads with 14 point font, and I include a component which lets the user resize the component to 16 point font as above. When I run this on the computer I compiled it on (Debian), it works as intended, but I tested it on a Mac and a Windows computer and the font resizing feature doesn't work at all. However, on all platforms the font is serif. Any ideas why this is happening?

解决方案

The component's default UI delegate on your platform may be pre-empting your setting. In the alternative, consider one of the following:

  • Use deriveFont() on the existing font, illustrated here.

    component.getFont().deriveFont(16f)
    

  • Use an available sizeVariant, illustrated here.

  • Use a custom UI delegate, illustrated here and here.

这篇关于Java/Swing字体调整大小在跨平台上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:21