统一设置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里面配置的属性。