解决Swing控件setEnabled(false)时的字体颜色

https://blog.csdn.net/u012505618/article/details/50125363

在设置Swing输入控件(主要是JCombobox、JTextField、JSpinner、JTextArea等)不可用时,默认字体颜色都是灰色,即Gray,这种颜色看不清楚,所以想改变输入控件不可用时的字体颜色。

去网上寻找到方法,使用UIManager类可以办到。例如在本例中,解决方法为:

UIManager.put(“TextField.inactiveForeground”, new Color(0, 0, 0));
1
更多Key可参考:http://bbs.csdn.net/topics/90029955

借此机会,学习一下UIManager类。

javax.swing.UIManager类就是Swing界面管理的核心,管理Swing的小应用程序以及应用程序样式的状态,与之密切相关的是LookAndFeel抽象类。LookAndFeel是一个抽象类,除了提供了一些static方法,还定义了一些抽象的个性化设置方法来由子类实现。

从JDK1.1.3开始,Sun提供了三个LookAndFeel的子类 :javax.swing.plaf.metal.MetalLookAndFeel、com.sun.java.swing.plaf.motif.MotifLookAndFeel、com.sun.java.swing.plaf.windows. WindowsLookAndFeel。它们分别提供了“Metal”、“Motif”与“Windows”的界面式样。也就是说,任何基于Swing的界面程序本身都可以使用三种系统提供的皮肤。

UIManager类提供的方法有(如果 key 的值不为 相应的类对象,则返回 null。):
1:setLookAndFeel(LookAndFeel newLookAndFeel),返回void,设置当前外观,不常用。
2: setLookAndFeel(String className),返回void,设置当前外观,常用, className为外观类LookAndFeel的完全限定名称。注意:一旦更改了外观,则强制在所有 JComponent 上调用 updateUI,SwingUtilities.updateComponentTreeUI(java.awt.Component) 可以一次性更新指定的组件外观,所以当用UIManager.setLookAndFeel(“javax.swing.plaf.metal.MetalLookAndFeel”)设置外观后必须用SwingUtilities.updateComponentTreeUI(java.awt.Component)更新指定的组件外观,否则会发生不可预知的错误。
3: addAuxiliaryLookAndFeel(LookAndFeel laf),返回void,将 LookAndFeel 添加到辅助外观的列表中。
4:removeAuxiliaryLookAndFeel(LookAndFeel laf),返回boolean,从辅助外观列表移除一个LookAndFeel。这些辅助外观会告知多路外观,在创建多路 UI 时除默认的 LookAndFeel 类外还使用其他哪些 LookAndFeel 类。该项更改只有在创建新的 UI 类或在组件实例上更改默认外观时生效。注意,这些类不同于已安装的外观,如果该 LookAndFeel 已从列表移除,则返回 true。
5:put(Object key, Object value),返回Object,在开发人员默认值中存储一个对象。此方法是 getDefaults().put(key, value) 的一个覆盖方法。此方法仅对开发人员默认值有效,对系统默认值和外观默认值无效。 简单地说就是把一个组件抽出来设置它的一些属性,比如设置全局组件的字体:
Font font = new Font(“Dialog”,Font.PLAIN,12);
UIManager.put(“ToolTip.font”,font);
UIManager.put(“Table.font”,font);
UIManager.put(“TableHeader.font”,font);
UIManager.put(“TextField.font”,font);
UIManager.put(“ComboBox.font”,font);
UIManager.put(“TextField.font”,font);
UIManager.put(“PasswordField.font”,font);
UIManager.put(“TextArea.font”,font);
UIManager.put(“TextPane.font”,font);
UIManager.put(“EditorPane.font”,font);
UIManager.put(“FormattedTextField.font”,font);
UIManager.put(“Button.font”,font);
UIManager.put(“CheckBox.font”,font);
UIManager.put(“RadioButton.font”,font);
UIManager.put(“ToggleButton.font”,font);
UIManager.put(“ProgressBar.font”,font);
UIManager.put(“DesktopIcon.font”,font);
UIManager.put(“TitledBorder.font”,font);
UIManager.put(“Label.font”,font);
UIManager.put(“List.font”,font);
UIManager.put(“TabbedPane.font”,font);
UIManager.put(“MenuBar.font”,font);
UIManager.put(“Menu.font”,font);
UIManager.put(“MenuItem.font”,font);
UIManager.put(“PopupMenu.font”,font);
UIManager.put(“CheckBoxMenuItem.font”,font);
UIManager.put(“RadioButtonMenuItem.font”,font);
UIManager.put(“Spinner.font”,font);
UIManager.put(“Tree.font”,font);
UIManager.put(“ToolBar.font”,font);
UIManager.put(“OptionPane.messageFont”,font);
UIManager.put(“OptionPane.buttonFont”,font);
6:get(Object key)或get(Object key, Locale l),返回Object,从默认值中返回一个对象。参数: key - 指定所需对象的 Object,l - 需要该对象的 Locale。
7:getBoolean(Object key)或getBoolean(Object key, Locale l),返回boolean,从与键值相关的默认值中返回一个布尔变量。如果找不到键或该键不表示布尔值,则返回 false。
8:getBorder(Object key)或getBorder(Object key, Locale l),返回Border
9:getColor(Object key)或getColor(Object key, Locale l)返回Color
10:getFont(Object key)或getFont(Object key, Locale l),返回Font
11:getDimension(Object key)或getDimension(Object key, Locale l),返回维数
12:getIcon(Object key)或getIcon(Object key, Locale l),返回Icon
13:getInsets(Object key)或getInsets(Object key, Locale l)返回Insets
14:getString(Object key)或getString(Object key, Locale l),返回String,从默认值中返回一个字符串。
15:UIManager.LookAndFeelInfo[] getInstalledLookAndFeels(),返回表示当前可用的 LookAndFeel 实现的 LookAndFeelInfo 数组。应用程序可以使用 LookAndFeelInfo 对象为用户构造外观选项的菜单,或确定在启动时要设置哪个外观。要避免创建众多 LookAndFeel 对象的影响,LookAndFeelInfo 维护 LookAndFeel 类的类名称,而不是实际的 LookAndFeel 实例。以下示例演示了如何根据 LookAndFeelInfo 的实例设置外观:
UIManager.setLookAndFeel(info.getClassName());
16:返回实现默认的跨平台外观 – Java Look and Feel (JLF) – 的 LookAndFeel 类的名称。可通过设置 swing.crossplatformlaf 系统属性重写此值。
17: getDefaults(),返回UIDefaults,该默认值。使用类文档中指定的逻辑解析返回的默认值。
18:getInt(Object key)或getInt(Object key, Locale l),返回int,从默认值中返回一个整数。
19:installLookAndFeel(UIManager.LookAndFeelInfo info),返回void,将指定的外观添加到可用外观的集合中。尽管此方法允许 null info,但建议最好使用非 null 值。
20:getLookAndFeel(),返回LookAndFeel,返回当前外观或 null。
21:public static PropertyChangeListener[] getPropertyChangeListeners()返回添加到此 UIManager(具有 addPropertyChangeListener())的所有 PropertyChangeListener 的数组。
22:public static void addPropertyChangeListener(PropertyChangeListener listener),将一个 PropertyChangeListener 添加到侦听器列表。该侦听器为所有属性进行注册。
23:public static void removePropertyChangeListener(PropertyChangeListener listener)从侦听器列表移除 PropertyChangeListener。此方法移除了一个为所有属性注册的 PropertyChangeListener。

猜你喜欢

转载自blog.csdn.net/ygl19920119/article/details/80006611