Java 更改整个 JFrame 的字体类型和大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9753875/
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
Change Font type and size for the whole JFrame?
提问by Ankit
I am working on Java GUI project. I have several components on the swing-based GUI. I want to change the font settings for all the components, rather than changing font for each component one by one.
我正在研究 Java GUI 项目。我在基于 Swing 的 GUI 上有几个组件。我想更改所有组件的字体设置,而不是一个一个更改每个组件的字体。
Is there a way to change the font type and size of all the components on a JPanel on a JFrame.
有没有办法更改 JFrame 上 JPanel 上所有组件的字体类型和大小。
Edit:
编辑:
This is how my code looks:
这是我的代码的样子:
public class Test extends JFrame{
public Test(){
//all components are initialized here. (some buttons and text fields) and added to this
this.setVisible(true);
}
public static void main(String []args){
new Test();
}
}
采纳答案by Eng.Fouad
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);
}
}
// ...
try
{
setUIFont(new javax.swing.plaf.FontUIResource("Tahoma",Font.PLAIN,12));
}
catch(Exception e){}
PS: I just copied this from old project of mine, and I am not sure from where I got it.
PS:我刚刚从我的旧项目中复制了这个,我不确定从哪里得到它。
回答by mKorbel
You can set this Font
into UIManager
, but for every type of JComponents
becase each of them have got own syntax in UIManager Defaultsand here
您可以将其设置Font
为UIManager
,但对于每种类型,JComponents
因为它们中的每一种在UIManager Defaults和此处都有自己的语法