从命令行更改 Swing 字体大小 [英] Change Swing font size from command line

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

问题描述

我正在使用 Swing 应用程序,该应用程序在我的计算机上以小得离谱的字体大小显示文本.

I am using a Swing application which on my computer shows text with a ridiculously small font size.

有没有办法从命令行或某种配置文件(例如,类似于 swing.properties 文件)更改字体大小或某种 DPI 设置)?

Is there a way to change the font size, or maybe some kind of DPI setting, from the command line or with some kind of configuration file (for example, something like a swing.properties file)?

我无权访问源代码.

自 Java 9 以来,小字体不再是问题.Swing 已开始根据屏幕分辨率缩放其 GUI 组件.

Small font sizes should not be a problem any more since Java 9. Swing has started to scale its GUI components depending on the screen resolution.

推荐答案

没有用于更改 Swing 字体大小的命令行开关.您需要做的是调用以下方法:

There is no command line switch to change the font size for Swing. What you would have to do is to invoke the following method:

public static void adjustFontSize(int adjustment) {

    UIDefaults defaults = UIManager.getDefaults();
    List<Object> newDefaults = new ArrayList<Object>();
    Map<Object, Font> newFonts = new HashMap<Object, Font>();

    Enumeration<Object> en = defaults.keys();
    while (en.hasMoreElements()) {
        Object key = en.nextElement();
        Object value = defaults.get(key);
        if (value instanceof Font) {
            Font oldFont = (Font)value;
            Font newFont = newFonts.get(oldFont);
            if (newFont == null) {
                newFont = new Font(oldFont.getName(), oldFont.getStyle(), oldFont.getSize() + adjustment);
                newFonts.put(oldFont, newFont);
            }
            newDefaults.add(key);
            newDefaults.add(newFont);
        }
    }
    defaults.putDefaults(newDefaults.toArray());
}

其中 adjustment 是应该添加到每个字体大小的点数.

where adjustment is the number of points that should be added to each font size.

如果您无权访问源代码,您始终可以在调用的地方编写自己的包装器主类

If you don't have access to the source code, you can always write your own wrapper main class where you call

    UIManager.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent event) {
            if (event.getPropertyName().equals("lookAndFeel")) {
                adjustFontSize(5);
            }
        }
    });

在调用实际应用程序的 main 方法之前.

before invoking the main method of the actual application.

但是,如果字体大小非常小,则可能已经明确设置,因此更改默认值可能无济于事.

However, if the font size is very small, it has likely been set explicitly, so changing the defaults might not help.

这篇关于从命令行更改 Swing 字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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