70

我想知道如何为我的整个 Java swing 程序设置默认字体。根据我的研究,它似乎可以用 来完成UIManager,与 相关LookAndFeel,但我找不到具体的方法,而且UIManager看起来很复杂。

4

12 回答 12

68

尝试:

public static void setUIFont (javax.swing.plaf.FontUIResource f){
    java.util.Enumeration 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);
      }
    } 

打电话...

setUIFont (new javax.swing.plaf.FontUIResource("Serif",Font.ITALIC,12));
于 2011-09-15T17:21:18.117 回答
41
UIManager.put("Button.font", /* font of your liking */);
UIManager.put("ToggleButton.font", /* font of your liking */);
UIManager.put("RadioButton.font", /* font of your liking */);
UIManager.put("CheckBox.font", /* font of your liking */);
UIManager.put("ColorChooser.font", /* font of your liking */);
UIManager.put("ComboBox.font", /* font of your liking */);
UIManager.put("Label.font", /* font of your liking */);
UIManager.put("List.font", /* font of your liking */);
UIManager.put("MenuBar.font", /* font of your liking */);
UIManager.put("MenuItem.font", /* font of your liking */);
UIManager.put("RadioButtonMenuItem.font", /* font of your liking */);
UIManager.put("CheckBoxMenuItem.font", /* font of your liking */);
UIManager.put("Menu.font", /* font of your liking */);
UIManager.put("PopupMenu.font", /* font of your liking */);
UIManager.put("OptionPane.font", /* font of your liking */);
UIManager.put("Panel.font", /* font of your liking */);
UIManager.put("ProgressBar.font", /* font of your liking */);
UIManager.put("ScrollPane.font", /* font of your liking */);
UIManager.put("Viewport.font", /* font of your liking */);
UIManager.put("TabbedPane.font", /* font of your liking */);
UIManager.put("Table.font", /* font of your liking */);
UIManager.put("TableHeader.font", /* font of your liking */);
UIManager.put("TextField.font", /* font of your liking */);
UIManager.put("PasswordField.font", /* font of your liking */);
UIManager.put("TextArea.font", /* font of your liking */);
UIManager.put("TextPane.font", /* font of your liking */);
UIManager.put("EditorPane.font", /* font of your liking */);
UIManager.put("TitledBorder.font", /* font of your liking */);
UIManager.put("ToolBar.font", /* font of your liking */);
UIManager.put("ToolTip.font", /* font of your liking */);
UIManager.put("Tree.font", /* font of your liking */);

来源:http ://www.jguru.com/faq/view.jsp?EID=340519

于 2011-09-15T17:18:20.880 回答
20
java -Dswing.aatext=true -Dswing.plaf.metal.controlFont=Tahoma -Dswing.plaf.metal.userFont=Tahoma …

这不仅会在你的完整 UI 上设置 Tahoma,还会打开抗锯齿,让任何字体立即变得更加漂亮。

于 2011-09-15T18:15:16.683 回答
12

我认为这更好,为当前的 laf 而不是整个 UIManager 调用它

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

在实例化您的 JFrame 对象之前的某个地方。它对我来说非常有效。请记住,对于没有指定字体的组件,这是默认字体。

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

于 2012-07-24T12:51:32.100 回答
5

Be aware that the way to set the default font depends on the Look And Feel you're using. The solution described by Romain Hippeau works fine with a lot of LAF but not with Nimbus. The one posted by sherif works fine with Nimbus, but not with others (Metal, for instance).

Combining both could work on most of LAF, but none of these solutions works with GTK+ LAF.

I think (but I'm not sure), there's no cross-platform solution.

于 2014-06-22T15:48:16.423 回答
4

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);
    }
}
于 2013-08-15T18:20:13.473 回答
2

As a completion of @Amir answer, this is the complete list of keys

I use this function

private void setFont(FontUIResource myFont) {
    UIManager.put("CheckBoxMenuItem.acceleratorFont", myFont);
    UIManager.put("Button.font", myFont);
    UIManager.put("ToggleButton.font", myFont);
    UIManager.put("RadioButton.font", myFont);
    UIManager.put("CheckBox.font", myFont);
    UIManager.put("ColorChooser.font", myFont);
    UIManager.put("ComboBox.font", myFont);
    UIManager.put("Label.font", myFont);
    UIManager.put("List.font", myFont);
    UIManager.put("MenuBar.font", myFont);
    UIManager.put("Menu.acceleratorFont", myFont);
    UIManager.put("RadioButtonMenuItem.acceleratorFont", myFont);
    UIManager.put("MenuItem.acceleratorFont", myFont);
    UIManager.put("MenuItem.font", myFont);
    UIManager.put("RadioButtonMenuItem.font", myFont);
    UIManager.put("CheckBoxMenuItem.font", myFont);
    UIManager.put("OptionPane.buttonFont", myFont);
    UIManager.put("OptionPane.messageFont", myFont);
    UIManager.put("Menu.font", myFont);
    UIManager.put("PopupMenu.font", myFont);
    UIManager.put("OptionPane.font", myFont);
    UIManager.put("Panel.font", myFont);
    UIManager.put("ProgressBar.font", myFont);
    UIManager.put("ScrollPane.font", myFont);
    UIManager.put("Viewport.font", myFont);
    UIManager.put("TabbedPane.font", myFont);
    UIManager.put("Slider.font", myFont);
    UIManager.put("Table.font", myFont);
    UIManager.put("TableHeader.font", myFont);
    UIManager.put("TextField.font", myFont);
    UIManager.put("Spinner.font", myFont);
    UIManager.put("PasswordField.font", myFont);
    UIManager.put("TextArea.font", myFont);
    UIManager.put("TextPane.font", myFont);
    UIManager.put("EditorPane.font", myFont);
    UIManager.put("TabbedPane.smallFont", myFont);
    UIManager.put("TitledBorder.font", myFont);
    UIManager.put("ToolBar.font", myFont);
    UIManager.put("ToolTip.font", myFont);
    UIManager.put("Tree.font", myFont);
    UIManager.put("FormattedTextField.font", myFont);
    UIManager.put("IconButton.font", myFont);
    UIManager.put("InternalFrame.optionDialogTitleFont", myFont);
    UIManager.put("InternalFrame.paletteTitleFont", myFont);
    UIManager.put("InternalFrame.titleFont", myFont);
}

and i call it in main before invoking the ui

setFont(new FontUIResource(new Font("Cabin", Font.PLAIN, 14)));

For a complete list of Swing UI Manager keys check this link

于 2018-05-02T02:20:55.803 回答
1

The correct answer is the one given by Amir Raminfar but you have to encapsulate the font as a FontUIResource.

For example:

UIManager.put("Button.font", new FontUIResource(new Font ("Helvetica", Font.BOLD, 16)));
于 2016-04-23T11:44:26.870 回答
0

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)

于 2014-08-21T16:28:58.107 回答
0

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);
于 2015-06-13T13:32:50.567 回答
0

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.

于 2016-12-13T09:47:37.120 回答
-1

None of these solutions work fine for me, I built 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);
        }
    }
}
于 2015-09-11T09:26:27.363 回答