简体   繁体   English

Java 中是否有任何内置方法来增加字体大小?

[英]Are there any built-in methods in Java to increase Font size?

Java 中是否有任何内置方法来增加字体大小?

The Font class allows you to specify font size. Font类允许您指定字体大小。

So, to create a font you do something like this:因此,要创建字体,您可以执行以下操作:

Font f = new Font("serif", Font.PLAIN, fontSize);

The fontSize parameter will determine the size of your Font . fontSize参数将确定Font的大小。

You can't actually change the size of an existing Font object.您实际上无法更改现有 Font 对象的大小。 The best way to achieve a similar effect is to use the deriveFont(size) method to create a new almost identical Font that is a different size.实现类似效果的最佳方法是使用deriveFont(size)方法创建一个新的几乎相同的大小不同的Font

Font biggerFont = existingFont.deriveFont(bigNumber);

You can derive a new Font with a different size by using the following:您可以使用以下方法派生出具有不同大小的新字体:

Font original = // some font
Font bigger = original.deriveFont(newSize);

Where newSize is a float, not an int.其中 newSize 是一个浮点数,而不是一个整数。 This is well documented in the JavaDoc for Font as other people have pointed out正如其他人指出的那样,这在 JavaDoc for Font 中有详细记录

Assuming that you want to change the font size on a specific JLabel , you can do:假设您想更改特定JLabel上的字体大小,您可以执行以下操作:

label.setFont(label.getFont().deriveFont(newSize));

Make sure that newSize is a float not an int .确保newSizefloat而不是int

I interpreted this question as "How can I increase font size for Swing across the board."我将这个问题解释为“如何全面增加 Swing 的字体大小”。 I'm not aware of any built-in way to do this, but you could do it yourself by modifying the values in the UIManager class on startup before you create any Swing components.我不知道有任何内置方法可以做到这一点,但您可以通过在创建任何 Swing 组件之前在启动时修改 UIManager 类中的值来自己完成。

I do this by having a parameter passed into my app that I use as a multiplier.我通过将一个参数传递到我用作乘数的应用程序中来做到这一点。 If I pass in 150 it'll multiply all existing fonts by 150%.如果我传入 150,它会将所有现有字体乘以 150%。 The code is as follows代码如下

public static void initializeFontSize() {
    String fontSizeParam = System.getProperty("myapp.fontSize");
    if (fontSizeParam != null) {
        float multiplier = Integer.parseInt(fontSizeParam) / 100.0f;
        UIDefaults defaults = UIManager.getDefaults();
        int i = 0;
        for (Enumeration e = defaults.keys(); e.hasMoreElements(); i++) {
            Object key = e.nextElement();
            Object value = defaults.get(key);
            if (value instanceof Font) {
                Font font = (Font) value;
                int newSize = Math.round(font.getSize() * multiplier);
                if (value instanceof FontUIResource) {
                    defaults.put(key, new FontUIResource(font.getName(), font.getStyle(), newSize));
                } else {
                    defaults.put(key, new Font(font.getName(), font.getStyle(), newSize));
                }
            }
        }
    }
}

you can set the property swing.plaf.metal.controlFont when running you application:您可以在运行应用程序时设置属性swing.plaf.metal.controlFont

java -Dswing.plaf.metal.controlFont=Dialog-50 YourMainClass java -Dswing.plaf.metal.controlFont=Dialog-50 YourMainClass

in this example, you set the default font to be "Dialog" with size 50.在本例中,您将默认字体设置为“Dialog”,大小为 50。

The question is way too vague to give a good answer.这个问题太含糊了,无法给出很好的答案。 But I think you want to systematically increase font size in your application.但我认为您想系统地增加应用程序中的字体大小。

The font face, style and size in a Java Swing application is controlled via the LookAndFeel mechanism. Java Swing 应用程序中的字体、样式和大小是通过 LookAndFeel 机制控制的。 You need to change the font in the look-and-feel if you want the change to apply to all Swing components of a given type.如果您希望更改应用于给定类型的所有 Swing 组件,则需要更改外观中的字体。

Have a look at the UIManager example.看看UIManager示例。

Here's how to change the font globally for some UI components:以下是如何为某些 UI 组件全局更改字体:

UIManager.put("Label.font", new FontUIResource(new Font("Dialog", Font.PLAIN, 10)));
UIManager.put("Button.font", new FontUIResource(new Font("Dialog", Font.BOLD, 10)));
UIManager.put("TextField.font", new FontUIResource(new Font("Dialog", Font.PLAIN, 10)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM