Setting the Default Font of Swing Program

2019-01-02 23:19发布

I was wondering how to set the default font for my entire Java swing program. From my research it appears it can be done with UIManager, something to do with LookAndFeel, but I can't find specifically how to do it, and the UIManager appears pretty complicated.

12条回答
We Are One
2楼-- · 2019-01-02 23:27

I think this is better, calling it for the current laf instead of the whole UIManager put this

UIManager.getLookAndFeelDefaults()
        .put("defaultFont", new Font("Arial", Font.BOLD, 14));

Somewhere in the main before instantiating your JFrame object. It worked perfectly for me. Remember this is the default font, for the components that have no specified font.

source: http://www.java.net/node/680725

查看更多
欢心
3楼-- · 2019-01-02 23:29

To solve this problem, I just implement AWTEventListener and listen for COMPONENT_ADDED of ContainerEvent.

All story description at: http://wiki.idempiere.org/en/Swing_Miss_Support_Some_Language

All code at: https://bitbucket.org/hieplq/unicentapos/src/9b22875ab65e26ff46fd9ae62d556b7f64621afa/src-extend/vn/hsv/uitil/font/FontGlyphsUtil.java?at=tip

  1. Implement AWTEventListener

 

public void eventDispatched(AWTEvent event) {
    if (!isMissSupportGlyph || !(event instanceof ComponentEvent) || !(event instanceof ContainerEvent))
        return;

    if (event instanceof ContainerEvent){
        ContainerEvent containerEvent = (ContainerEvent)event;
        if (containerEvent.getID() == ContainerEvent.COMPONENT_ADDED){
            updateChildControlFont(containerEvent.getChild());
        }
    }
}
  1. Add registry listener (the best place to run this is when starting the program)

 

Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.COMPONENT_EVENT_MASK | AWTEvent.CONTAINER_EVENT_MASK);
查看更多
Emotional °昔
4楼-- · 2019-01-02 23:31

I'm using Nimbus L&F.

Using code from @Romain Hippeau, I had to use UIManager.getLookAndFeelDefaults() instead of UIManager.getDefaults() and use the returned reference to put modified values:

    int szIncr = 5; // Value to increase the size by
    UIDefaults uidef = UIManager.getLookAndFeelDefaults();
    for (Entry<Object,Object> e : uidef.entrySet()) {
        Object val = e.getValue();
        if (val != null && val instanceof FontUIResource) {
            FontUIResource fui = (FontUIResource)val;
            uidef.put(e.getKey(), new FontUIResource(fui.getName(), fui.getStyle(), fui.getSize()+szIncr));
        }
    }

For some reason, it does not seem to work with the default L&F... (based on the limited tests I performed)

查看更多
Deceive 欺骗
5楼-- · 2019-01-02 23:32

I used the Synth look and feel XML file and defined the fonts there. Easy, flexible and continent.
You need to create a class with a createFont like:

public class CustomFontResource {
public static FontUIResource createFont(String path, final int size) throws IOException, FontFormatException {
    Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(path));
    return new FontUIResource(font.deriveFont(Font.PLAIN, size));
}

And in your synth xml define the font like:

    <object id="Basic_Regular" class="<your CustomFontResource class>"
        method="createFont">
    <string>path_to_your_font</string>
    <int>font_size</int>
</object>

then you may use it as a reference wherever you want in the xml.

查看更多
欢心
6楼-- · 2019-01-02 23:35

none of theses solutions work fine for me, I build my own (stupid) one but it works:

private void changeFontRecursive(Container root, Font font) {
    for (Component c : root.getComponents()) {
        c.setFont(font);
        if (c instanceof Container) {
            changeFontRecursive((Container) c, font);
        }
    }
}
查看更多
一夜七次
7楼-- · 2019-01-02 23:37

Inspired by Romain Hippeau, use this code if you want to set just the font size.

for (Map.Entry<Object, Object> entry : javax.swing.UIManager.getDefaults().entrySet()) {
    Object key = entry.getKey();
    Object value = javax.swing.UIManager.get(key);
    if (value != null && value instanceof javax.swing.plaf.FontUIResource) {
        javax.swing.plaf.FontUIResource fr=(javax.swing.plaf.FontUIResource)value;
        javax.swing.plaf.FontUIResource f = new javax.swing.plaf.FontUIResource(fr.getFamily(), fr.getStyle(), FONT_SIZE);
        javax.swing.UIManager.put(key, f);
    }
}
查看更多
登录 后发表回答