Change Swing font size from command line

好久不见. 提交于 2019-12-14 03:47:32

问题


I am using a Swing application which on my computer shows text with a ridiculously small font size. I don't have access to the source code.

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)?


EDIT:

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


回答1:


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

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

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.




回答2:


I don't think there are such options since all the component Font sizes are defined in Look and Feel (L&F) default values. Some of L&Fs allow quick font changes, some of them doesn't. In most cases you should be able change the font size by changing the UI defaults:

UIManager.put ( "Button.font", new SwingLazyValue ( "javax.swing.plaf.FontUIResource", null, new Object[]{ fontName, fontStyle, fontSize } ) );
UIManager.put ( "Label.font", new SwingLazyValue ( "javax.swing.plaf.FontUIResource", null, new Object[]{ fontName, fontStyle, fontSize } ) );
UIManager.put ( "TextField.font", new SwingLazyValue ( "javax.swing.plaf.FontUIResource", null, new Object[]{ fontName, fontStyle, fontSize } ) );

e.t.c for each component.

And i am not sure if those values could be passed to application without changing its code or atleast having some font-size change support inside the application.




回答3:


I have not tested it, but you might try launching the app. using Java Web Start. It allows properties like swing.useSystemFontSettings & swing.metalTheme to be specified even for sand-boxed apps. Doing either might 'override' small fonts set in the code.

See the JNLP file syntax for more details.



来源:https://stackoverflow.com/questions/12422048/change-swing-font-size-from-command-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!