Java 中是否有任何内置方法来增加字体大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1043872/
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
Are there any built-in methods in Java to increase Font size?
提问by
Are there any built-in methods in Java to increase Font size?
Java 中是否有任何内置方法来增加字体大小?
回答by jjnguy
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. 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.
您实际上无法更改现有 Font 对象的大小。实现类似效果的最佳方法是使用该deriveFont(size)
方法创建一个Font
大小不同的几乎相同的新方法。
Font biggerFont = existingFont.deriveFont(bigNumber);
回答by tddmonkey
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, notan int. This is well documented in the JavaDoc for Font as other people have pointed out
其中 newSize 是一个浮点数,而不是一个整数。正如其他人指出的那样,这在 JavaDoc for Font 中有详细记录
回答by Avrom
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
.
确保newSize
是float
不是int
。
回答by cd1
you can set the property swing.plaf.metal.controlFontwhen 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。
回答by CarlG
I interpreted this question as "How can I increase font size for Swing across the board." 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 的字体大小”。我不知道有任何内置方法可以做到这一点,但您可以通过在创建任何 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%. The code is as follows
我通过将一个参数传递到我用作乘数的应用程序中来做到这一点。如果我传入 150,它会将所有现有字体乘以 150%。代码如下
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));
}
}
}
}
}
回答by rustyx
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. 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.
Java Swing 应用程序中的字体、样式和大小是通过 LookAndFeel 机制控制的。如果您希望更改应用于给定类型的所有 Swing 组件,则需要更改外观中的字体。
Have a look at the UIManagerexample.
看看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)));