为所有组件 Java 设置相同的字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12730230/
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
Set the same font for all component Java
提问by Luca
I want to set a specific font for all component in a JPanel
but I prefer that the question is still more general and not limited to the JPanel. How can I set the font to a list of component in a container (JFrame or JPanel)?
我想为 a 中的所有组件设置一个特定的字体,JPanel
但我更喜欢这个问题更笼统,不限于 JPanel。如何将字体设置为容器(JFrame 或 JPanel)中的组件列表?
回答by Mikle Garin
Here is a simple method that allows you to specify Font to the whole components tree under any Container (or just a simple Component, doesn't matter):
这是一个简单的方法,它允许您将 Font 指定给任何 Container 下的整个组件树(或只是一个简单的组件,无所谓):
public static void changeFont ( Component component, Font font )
{
component.setFont ( font );
if ( component instanceof Container )
{
for ( Component child : ( ( Container ) component ).getComponents () )
{
changeFont ( child, font );
}
}
}
Simply pass your panel and specific Font into this method and you will get all childs refactored aswell.
只需将您的面板和特定的 Font 传递到此方法中,您就会重构所有孩子。
回答by Kumar Vivek Mitra
-You can use UIManager
to do this....
-你可以UIManager
用来做这个....
Eg:
例如:
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);
}
}
回答by Brian
Set the font values in UIManager
for the components you want to change. For example, you can set the font used for labels by doing:
UIManager
为要更改的组件设置字体值。例如,您可以通过执行以下操作来设置用于标签的字体:
Font labelFont = ... ;
UIManager.put("Label.font", labelFont);
Note that different look and feels (L&F) may have different properties for the UIManager
class, so if you switch from one L&F to another, you may have issues.
请注意,不同的外观 (L&F) 可能具有不同的UIManager
类属性,因此如果您从一个 L&F 切换到另一个,您可能会遇到问题。
回答by Deepak Odedara
Inspired from Mikle Grains Answer I used his code to increase the font of each component in percentage by getting the old fontsize
受 Mikle Grains Answer 的启发,我使用他的代码通过获取旧字体大小来增加每个组件的字体百分比
public static void changeFont(Component component, int fontSize) {
Font f = component.getFont();
component.setFont(new Font(f.getName(),f.getStyle(),f.getSize() + fontSize));
if (component instanceof Container) {
for (Component child : ((Container) component).getComponents()) {
changeFont(child, fontSize);
}
}
}