java 更改 JPanel 及其所有元素的字体大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2989107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:50:34  来源:igfitidea点击:

Change font size of a JPanel and all its elements

javaswing

提问by YuppieNetworking

I am trying to create a Swing panel whose elements have a different font size than the rest of the swing application. Initially, using setFontfor a couple of components didn't pose any problems. Now I have several components (and all their subcomponents), so this solution is impractical.

我正在尝试创建一个 Swing 面板,其元素的字体大小与 Swing 应用程序的其余部分不同。最初,setFont用于几个组件不会造成任何问题。现在我有几个组件(以及它们的所有子组件),所以这个解决方案是不切实际的。

I have searched about changing the default UI properties of swing components. What I have found is mostly using the UIManager, which changes the properties globally. This doesn't work for me because I want to keep the current font settings for all the other panels.

我搜索了有关更改 Swing 组件的默认 UI 属性的信息。我发现主要是使用 UIManager,它会全局更改属性。这对我不起作用,因为我想保留所有其他面板的当前字体设置。

For the moment (and since I don't like to post without trying something out first), I have an algorithm like this:

目前(并且因为我不喜欢在没有先尝试的情况下发帖),我有一个这样的算法:

public static void fixFont(Container c) {
    c.setFont(c.getFont().deriveFont(10.0f));
    Component[] comp = c.getComponents();
    for (int i=0;i<comp.length;++i) {
        if (comp[i] instanceof Container) {
            fixFont((Container) comp[i]);
        } else {
            comp[i].setFont(comp[i].getFont().deriveFont(10.0f));
        }
    }
}

The problem is that:

问题在于:

  • it doesn't include certain swing elements like its border.
  • I have to call this function when I add other components dynamically
  • 它不包括某些摆动元素,例如其边框。
  • 动态添加其他组件时必须调用此函数

Question:Is there another way to change the font properties of a Swing panel and all its components, elements, etc (i.e. everything in the panel) ?

问题:是否有另一种方法可以更改 Swing 面板及其所有组件、元素等(即面板中的所有内容)的字体属性?

Thanks for your ideas

谢谢你的想法

采纳答案by aioobe

You could use this trick:

你可以使用这个技巧:

import java.awt.*;

public class FrameTest {

    public static void setUIFont(FontUIResource f) {
        Enumeration keys = UIManager.getDefaults().keys();
        while (keys.hasMoreElements()) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof FontUIResource) {
                FontUIResource orig = (FontUIResource) value;
                Font font = new Font(f.getFontName(), orig.getStyle(), f.getSize());
                UIManager.put(key, new FontUIResource(font));
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {

        setUIFont(new FontUIResource(new Font("Arial", 0, 20)));

        JFrame f = new JFrame("Demo");
        f.getContentPane().setLayout(new BorderLayout());

        JPanel p = new JPanel();
        p.add(new JLabel("hello"));
        p.setBorder(BorderFactory.createTitledBorder("Test Title"));

        f.add(p);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300, 300);
        f.setVisible(true);
    }
}

Produces:

产生:

enter image description here

在此处输入图片说明

回答by pstanton

you could override the addmethod on your base component and apply the font to the added components and their children there. this would save you applying the font manually when components are added later.

您可以覆盖add基本组件上的方法并将字体应用于添加的组件及其子组件。这将节省您在稍后添加组件时手动应用字体。