在Swing应用程序中更改运行时的字体大小 [英] Changing font-size at runtime in a Swing application

查看:151
本文介绍了在Swing应用程序中更改运行时的字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在很多网站都可以选择让用户增加或减少网站上显示的字体大小。我想在我的Java Swing应用程序中添加类似的功能,可以选择将字体更改为更大的尺寸或恢复为标准尺寸。用户可以通过菜单项在运行时执行此操作。

A lot of web sites these days have an option to let the user increase or decrease the size of the font that appears on the site. I'd like to add similar functionality to my Java Swing app, with an option to change the font to either a larger size or revert to standard size. The user would be able to do this at runtime, via a menu item.

我的代码的当前版本运行不正常 - 代码遍历键存储在UIManager / UIDefaults中的每个键都是一个Font,我的代码派生出一个具有目标点大小的新字体(例如,如果它更大,那么新的点大小通常为16;否则,如果是常规大小,则为通常是11),然后将该键和新字体放入UIManager中。然后,代码调用框架上的SwingUtilities.updateComponentTreeUI(),然后打包框架。

The current version of my code isn't working very well - the code iterates over the keys stored in UIManager/UIDefaults, and for each key that's a Font, my code derives a new font with the target point size (e.g., if it's larger, then the new point size is usually 16; otherwise, if going to regular size, it's usually 11), and then puts that key and new font in UIManager. Afterwards, the code calls SwingUtilities.updateComponentTreeUI() on the frame, and then packs the frame.

代码将增加字体一次,但切换回常规尺寸已经没有效果。这在菜单栏中尤为明显。

The code will increase the font once, but switching back to regular size has no effect. This is especially noticeable with the menu bar.

我是否以正确的方式进行此操作?我应该使用更大的磅值来创建新的外观,还是完全使用其他方法?

Am I going about this the right way? Should I instead create a new look and feel with the larger point size, or use some other method entirely?

提前感谢您的帮助。

推荐答案


代码会增加字体一次,
但切换回常规尺寸有
否效果

The code will increase the font once, but switching back to regular size has no effect

我认为您需要使用FontUIResource更新UIManager,而不仅仅是字体。

I believe you need to update the UIManager with a FontUIResource, not just a Font.

这篇关于在Swing应用程序中更改运行时的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆