关于 UIManager.getColor(Object key) 的使用 Posted on 2004年12月16日 20:06 在做界面主题的变换的时候,我们经常要对系统的一些颜色进行默认设置,而获得在主题变换时,组件的颜色自动转换到该主题相应的颜色的一种效果。UIManager在这方面是一种很关键的应用。 在UIManager(Java 2 Platform SE v1.4.2)的方法public static Color getColor(Object key)注释中,key是一个指定颜色的对象,而具体包括哪些可以使用则不得知。 我们阅读jdk的源代码可以知道,运行UIManager.getColor的时候,其实是通过运行[return getDefaults().getColor(key); ]这段代码获得颜色的。而getDefaults() 返回了一个UIDefaults。再看看UIDefaults,它是通过[Object value = get(key);]来获得颜色的对象,通过它调用的最低层的代码,我们知道,这些对象列表是保存在一个Hashtable(UIDefaults继承了Hashtable)中的,而Hashtable提供了一个接口[public synchronized Enumeration keys()]来获得这些值。 因此,我们可以通过一段简单的代码来获得可以使用的颜色的关键对象。 import java.awt.*; import java.util.*; import javax.swing.*; public class UIManagerColorTest { public UIManagerColorTest() { } public static void main(String[] args) { UIDefaults uiDefaults = UIManager.getDefaults(); Enumeration enum = uiDefaults.keys(); while (enum.hasMoreElements()) { Object key = enum.nextElement(); Object val = uiDefaults.get(key); //如果是颜色对象,则打印 try { Color color = (Color) val; System.out.println("[" + key.toString() + "]"); } catch (ClassCastException cce) { } } } } 以下是运行程序后的结果: [DesktopIcon.background] [windowText] [activeCaptionBorder] [InternalFrame.borderColor] [ScrollBar.highlight] [ComboBox.disabledBackground] [Tree.selectionBackground] [FormattedTextField.inactiveBackground] [InternalFrame.inactiveTitleBackground] [Table.selectionForeground] [Label.foreground] [ToggleButton.highlight] [Button.background] [FormattedTextField.caretForeground] [RadioButtonMenuItem.selectionForeground] [ToggleButton.shadow] [OptionPane.errorDialog.titlePane.shadow] [EditorPane.background] [RadioButtonMenuItem.foreground] [RadioButton.light] [ToggleButton.focus] [OptionPane.warningDialog.titlePane.background] [TextArea.caretForeground] [Button.highlight] [CheckBox.focus] [Separator.foreground] [TabbedPane.light] [Tree.selectionBorderColor] [inactiveCaptionText] [ToolTip.backgroundInactive] [PasswordField.caretForeground] [EditorPane.inactiveForeground] [ToolTip.foreground] [ToolBar.dockingBackground] [ToggleButton.disabledText] [ToggleButton.background] [Slider.focus] [ToolBar.floatingForeground] [TabbedPane.background] [ComboBox.foreground] [ScrollBar.shadow] [RadioButtonMenuItem.acceleratorForeground] [InternalFrame.borderDarkShadow] [PopupMenu.background] [controlLtHighlight] [CheckBoxMenuItem.background] [TextPane.foreground] [FormattedTextField.background] [Viewport.background] [TextField.light] [MenuItem.background] [TabbedPane.tabAreaBackground] [Button.focus] [inactiveCaptionBorder] [CheckBoxMenuItem.selectionBackground] [textInactiveText] [OptionPane.errorDialog.titlePane.background] [TextField.inactiveBackground] [Tree.selectionForeground] [Menu.selectionBackground] [Tree.background] [InternalFrame.inactiveTitleForeground] [control] [SplitPane.background] [textText] [RadioButton.foreground] [menu] [EditorPane.foreground] [SplitPane.shadow] [OptionPane.warningDialog.titlePane.foreground] [TabbedPane.selectHighlight] [RadioButton.shadow] [Menu.disabledForeground] [ToggleButton.light] [windowBorder] [OptionPane.questionDialog.border.background] [ToggleButton.foreground] [Panel.background] [TabbedPane.foreground] [RadioButtonMenuItem.background] [Table.gridColor] [ScrollBar.foreground] [TextPane.caretForeground] [ToolBar.shadow] [ToggleButton.select] [Spinner.foreground] [FormattedTextField.foreground] [Viewport.foreground] [RadioButton.darkShadow] [infoText] [MenuItem.acceleratorSelectionForeground] [MenuItem.foreground] [MenuBar.background] [OptionPane.errorDialog.titlePane.foreground] [Button.light] [menuText] [ToggleButton.darkShadow] [TextPane.background] [MenuItem.acceleratorForeground] [TabbedPane.darkShadow] [CheckBox.background] [TextField.highlight] [TextArea.selectionBackground] [Panel.foreground] [OptionPane.questionDialog.titlePane.background] [RadioButton.background] [TextArea.inactiveForeground] [Slider.shadow] [textHighlightText] [SplitPane.darkShadow] [RadioButton.select] [PasswordField.foreground] [ScrollBar.thumb] [activeCaptionText] [MenuBar.foreground] [OptionPane.warningDialog.titlePane.shadow] [ScrollBar.background] [Menu.foreground] [CheckBox.disabledText] [Spinner.background] [TextPane.selectionForeground] [ComboBox.selectionBackground] [ScrollBar.track] [CheckBox.foreground] [TextField.caretForeground] [ScrollBar.thumbDarkShadow] [List.selectionBackground] [OptionPane.messageForeground] [TextPane.inactiveForeground] [Button.shadow] [Menu.acceleratorForeground] [TextArea.selectionForeground] [Menu.acceleratorSelectionForeground] [TextField.foreground] [textHighlight] [OptionPane.questionDialog.titlePane.foreground] [TabbedPane.selected] [controlShadow] [MenuItem.disabledForeground] [MenuItem.selectionForeground] [MenuItem.checkIcon] [ColorChooser.swatchesDefaultRecentColor] [ToolBar.highlight] [OptionPane.foreground] [controlText] [TextArea.background] [Tree.line] [CheckBoxMenuItem.disabledForeground] [Separator.highlight] [TextField.darkShadow] [Tree.textForeground] [ComboBox.selectionForeground] [ToolBar.foreground] [PasswordField.selectionBackground] [Label.disabledShadow] [FormattedTextField.selectionBackground] [PasswordField.background] [ProgressBar.background] [List.selectionForeground] [Checkbox.select] [MenuBar.highlight] [ComboBox.buttonShadow] [Menu.background] [info] [TextPane.selectionBackground] [PasswordField.inactiveBackground] [controlHighlight] [ScrollBar.darkShadow] [SplitPane.highlight] [TableHeader.background] [InternalFrame.activeTitleForeground] [TextArea.foreground] [TextField.background] [InternalFrame.borderShadow] [Button.select] [MenuItem.selectionBackground] [TextField.shadow] [ScrollBar.thumbShadow] [ScrollBar.thumbHighlight] [scrollbar] [window] [PasswordField.selectionForeground] [TextField.selectionForeground] [FormattedTextField.selectionForeground] [OptionPane.background] [Separator.shadow] [ComboBox.buttonBackground] [Table.focusCellBackground] [ColorChooser.background] [ProgressBar.foreground] [Table.foreground] [Tree.hash] [RadioButtonMenuItem.acceleratorSelectionForeground] [TitledBorder.titleColor] [inactiveCaption] [Slider.highlight] [Tree.textBackground] [EditorPane.selectionBackground] [ProgressBar.selectionForeground] [Button.disabledText] [ToolBar.background] [Label.disabledForeground] [PasswordField.inactiveForeground] [TabbedPane.shadow] [TabbedPane.highlight] [List.background] [InternalFrame.borderHighlight] [TableHeader.foreground] [Slider.background] [RadioButtonMenuItem.disabledForeground] [controlDkShadow] [OptionPane.questionDialog.titlePane.shadow] [DesktopIcon.foreground] [InternalFrame.activeTitleBackground] [CheckBoxMenuItem.acceleratorForeground] [Desktop.background] [Table.focusCellForeground] [ColorChooser.foreground] [RadioButton.highlight] [Menu.checkIcon] [OptionPane.errorDialog.border.background] [ComboBox.buttonHighlight] [ToolBar.light] [EditorPane.selectionForeground] [ComboBox.disabledForeground] [ScrollPane.background] [FormattedTextField.inactiveForeground] [CheckBoxMenuItem.acceleratorSelectionForeground] [activeCaption] [TextField.selectionBackground] [Button.foreground] [Table.background] [RadioButton.disabledText] [MenuBar.shadow] [List.foreground] [text] [desktop] [ComboBox.buttonDarkShadow] [ProgressBar.selectionBackground] [Slider.foreground] [ToolBar.dockingForeground] [ToolTip.foregroundInactive] [RadioButton.focus] [Table.selectionBackground] [ScrollBar.trackHighlight] [Label.background] [OptionPane.warningDialog.border.background] [TabbedPane.focus] [RadioButtonMenuItem.selectionBackground] [ToolBar.darkShadow] [Separator.background] [PopupMenu.foreground] [CheckBoxMenuItem.foreground] [EditorPane.caretForeground] [Button.darkShadow] [ToolTip.background] [CheckBoxMenuItem.selectionForeground] [ToolBar.floatingBackground] [ComboBox.background] [TextField.inactiveForeground] [ScrollPane.foreground] [Menu.selectionForeground] [InternalFrame.borderLight] [Tree.foreground] 同样的,我们可以修改上面的源代码以获得其他UIManager的其他获得特性的关键词。 参考文档:http://bdn.borland.com/article/0,1410,29991,00.html