Setting the Global Font for a Java Application

2020-06-17 05:03发布

问题:

I need to set the the default font for my application. Is there a way to do this that is not LaF dependent?

回答1:

Figured it out:

Call with: setUIFont (new javax.swing.plaf.FontUIResource(new Font("MS Mincho",Font.PLAIN, 12)));

private static void setUIFont(javax.swing.plaf.FontUIResource f)
{
    java.util.Enumeration<Object> keys = UIManager.getDefaults().keys();
    while (keys.hasMoreElements())
    {
        Object key = keys.nextElement();
        Object value = UIManager.get(key);
        if (value instanceof javax.swing.plaf.FontUIResource)
        {
            UIManager.put(key, f);
        }
    }
}


回答2:

for better control about how/which fonts to replace - in a LAF independent way, but controllable per-laf - have a look at the JGoodies Looks project

http://java.net/projects/looks

It allows to swap entire FontSets (that's a collection of semantic fonts, like control, dialog, message) at runtime.



标签: java swing fonts