我正在从事 Java GUI 项目。我在基于 swing 的 GUI 上有几个组件。我想更改所有组件的字体设置,而不是一个一个地更改每个组件的字体。
有没有办法更改 JFrame 上的 JPanel 上所有组件的字体类型和大小。
编辑:
这是我的代码的样子:
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();
}
}
最佳答案
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:我刚刚从我的旧项目中复制了这个,我不确定从哪里得到它。
关于java - 更改整个 JFrame 的字体类型和大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9753875/