【问题标题】:Setting the Default Font of Swing Program设置 Swing 程序的默认字体
【发布时间】:2011-11-18 02:08:37
【问题描述】:

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

【问题讨论】:

  • 如果您在此窗口中查看正确的站点,请向下滚动,我可以看到列Related 有 3-5 个关于类似问题的优秀主题

标签: java swing fonts uimanager


【解决方案1】:

尝试:

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

【讨论】:

  • 出于某种原因,这只是在我的 JTabbedPane 上更改选项卡文本上的字体。
  • 我让它使用 UIManager.getLookAndFeelDefaults() 而不是 UIManager.getDefaults() 并使用返回的引用而不是 UIManager.put() 工作(请参阅下面的答案)。它仅在覆盖默认 L&F 时才有效(在我的例子中是 Nimbus)...
【解决方案2】:
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

【讨论】:

  • 哇...通过将 New Font 对象添加到您喜欢的 /* 字体 */ 部分是否有效? Bcoz,似乎一旦JFRame创建并运行......我没有获得任何效果。 @Amir Raminfar 有什么我忘记的吗?
  • 来源来源:BasicLookAndFeel.java
  • 附加信息:一些组件有多个键来设置组件不同部分的字体。例如 JOptionPane:要为 JOptionPane 设置默认字体,您应该使用:UIManager.put("OptionPane.messageFont", /*font*/);UIManager.put("OptionPane.buttonFont", /*font*/);
  • @gumuruh 它在您刷新内容树时起作用SwingUtilities.updateComponentTreeUI(this);
【解决方案3】:
java -Dswing.aatext=true -Dswing.plaf.metal.controlFont=Tahoma -Dswing.plaf.metal.userFont=Tahoma …

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

【讨论】:

  • 效果很好。并设置名称中包含空格的字体,您可以引用。还可以通过添加“-”来指定字体大小:-Dswing.plaf.metal.controlFont="Droid Sans-15" (size 15)
  • 对我根本不起作用。引发无法识别的选项错误。
【解决方案4】:

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

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

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

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

【讨论】:

  • 适用于使用调色板、拖放制作的 netbeans swing 应用程序。
【解决方案5】:

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

【讨论】:

  • 更准确的方法是UIManager.put(key, new javax.swing.plaf.FontUIResource(fr.deriveFont(fr.getSize2D() * 2.0f)));
【解决方案6】:

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

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

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

【讨论】:

    【解决方案7】:

    作为@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 管理器键的完整列表请查看link

    【讨论】:

    • 注意在调用 ui 之前调用它是必要的,否则只有少数地方会出现字体效果。
    【解决方案8】:

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

    例如:

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

    【讨论】:

    • 对不起,我认为这段代码更好:UIManager.put("Button.font", new FontUIResource("Helvetica", Font.BOLD, 16));
    【解决方案9】:

    我正在使用 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...(基于我执行的有限测试)

    【讨论】:

      【解决方案10】:

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

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

      所有代码在: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);
      

      【讨论】:

        【解决方案11】:

        我使用了Synth look and feel 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 中的任何位置使用它作为参考。

        【讨论】:

          【解决方案12】:

          这些解决方案都不适合我,我构建了自己的(愚蠢的)一个,但它可以工作:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-01-18
            • 1970-01-01
            • 1970-01-01
            • 2011-03-09
            • 2012-02-01
            • 1970-01-01
            • 2011-10-12
            相关资源
            最近更新 更多