您的位置:首页 > 其它

统一设置Swing组件的背景色和字体

2012-06-21 07:13 197 查看
统一设置Swing组件的背景色和字体。可以在加载组件之前对组件的背景色和字体作统一的设置。设置代码如下:

Enumeration<Object> keys = UIManager.getDefaults().keys();
Object key = null;
Object value = null;
while (keys.hasMoreElements()) {
key = keys.nextElement();
value = UIManager.get(key);
if(key instanceof String ) {
/**设置全局的背景色*/
if(((String) key).endsWith(".background")) {
UIManager.put(key, Color.white);
}
}

/**设置全局的字体*/
if(value instanceof Font) {
UIManager.put(key, new Font(Font.DIALOG,Font.PLAIN,12));
}
}


通过上面的设置,swing组件的所有背景和字体都得到了统一。如果有组件需要设置自己的背景色或是字体时,在程序中可以通过调用setFont(Font font)、方法和setBackground(Color color)两个方法来实现,这两个方法将覆盖UIManager里面配置的属性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐