设置Swing程序的默认字体

发布于 2024-12-04 22:38:21 字数 159 浏览 0 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

爱殇璃 2024-12-11 22:38:21

尝试:

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

try:

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

Call by ...

setUIFont (new javax.swing.plaf.FontUIResource("Serif",Font.ITALIC,12));
醉殇 2024-12-11 22:38:21
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

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 */);

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

放肆 2024-12-11 22:38:21
java -Dswing.aatext=true -Dswing.plaf.metal.controlFont=Tahoma -Dswing.plaf.metal.userFont=Tahoma …

这不仅会在您的完整 UI 上设置 Tahoma,还会打开抗锯齿功能,使任何字体立即变得更加美观。

java -Dswing.aatext=true -Dswing.plaf.metal.controlFont=Tahoma -Dswing.plaf.metal.userFont=Tahoma …

This will not only set Tahoma on your complete UI but also turn on anti-aliasing which makes any font much more beautiful immediately.

葬シ愛 2024-12-11 22:38:21

我认为这更好,为当前的 laf 而不是整个 UIManager 调用它
在实例化 JFrame 对象之前,将其放在

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

main 中的某个位置。
它对我来说非常有效。
请记住,对于没有指定字体的组件,这是默认字体。

来源:http://www.java.net/node/680725

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

挽你眉间 2024-12-11 22:38:21

受 Romain Hippeau 的启发,如果您只想设置字体大小,请使用此代码。

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

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);
    }
}
栀子花开つ 2024-12-11 22:38:21

请注意,设置默认字体的方式取决于您使用的外观和感觉。
Romain Hippeau 描述的解决方案适用于许多 LAF,但不适用于 Nimbus。 sherif 发布的版本适用于 Nimbus,但不适用于其他版本(例如 Metal)。

将两者结合起来可以适用于大多数 LAF,但这些解决方案都不适用于 GTK+ LAF。

我认为(但我不确定),没有跨平台的解决方案。

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.

昨迟人 2024-12-11 22:38:21

的完整按键列表

作为 @Amir 答案的完成,这是我使用此函数

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

,并在调用 ui 之前在 main 中调用它。

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

有关 Swing UI Manager 键的完整列表 检查这个链接

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

奈何桥上唱咆哮 2024-12-11 22:38:21

正确的答案是 Amir Raminfar 给出的答案,但您必须将字体封装为 FontUIResource。

例如:

UIManager.put("Button.font", new FontUIResource(new Font ("Helvetica", Font.BOLD, 16)));

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)));
柠栀 2024-12-11 22:38:21

我正在使用 Nimbus L&F。

使用 @Romain Hippeau 的代码,我必须使用 UIManager.getLookAndFeelDefaults() 而不是 UIManager.getDefaults() 并使用返回的引用来 put 修改值:

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

由于某种原因,它似乎不适用于默认的 L&F...(基于我执行的有限测试)

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)

遮云壑 2024-12-11 22:38:21

为了解决这个问题,我只需实现 AWTEventListener 并监听 Co​​ntainerEvent 的 COMPONENT_ADDED 。

所有故事描述位于:http://wiki.idempiere.org/en/Swing_Miss_Support_Some_Language

所有代码位于:<一href="https://bitbucket.org/hieplq/unicentapos/src/9b22875ab65e26ff46fd9ae62d556b7f64621afa/src-extend/vn/hsv/uitil/font/FontGlyphsUtil.java?at=tip" rel="nofollow">https://bitbucket.org/hieplq/unicentapos/src/9b22875ab65e26ff46fd9ae62d556b7f64621afa/src-extend/vn/hsv/uitil/font/FontGlyphsUtil.java?at=tip

  1. 实现

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. 添加注册表监听器(最好在启动程序时运行

Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.COMPONENT_EVENT_MASK | AWTEvent.CONTAINER_EVENT_MASK);

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);
单身情人 2024-12-11 22:38:21

我使用了 Synth 外观 XML 文件并定义了那里有字体。简单、灵活、大陆。
您需要创建一个带有 createFont 的类,例如:

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

在您的合成 xml 中定义字体,例如:

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

然后您可以在 xml 中的任何位置使用它作为参考。

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.

笑,眼淚并存 2024-12-11 22:38:21

这些解决方案都不适合我,我构建了自己的(愚蠢的)解决方案,但它有效:

private void changeFontRecursive(Container root, Font font) {
    for (Component c : root.getComponents()) {
        c.setFont(font);
        if (c instanceof Container) {
            changeFontRecursive((Container) c, font);
        }
    }
}

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);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文