解決Swing控件setEnabled(false)時的字體顏色

最近接手了一個小項目,要用到java的swing開發(swing什麼的本人真的只在書上看到)。

然後就碰到了一個問題。在設置Swing輸入控件(主要是JCombobox、JTextField、JSpinner、JTextArea等)不可用時,默認字體顏色都是灰色,即Gray,這種顏色看不清楚,所以想改變輸入控件不可用時的字體顏色。

去網上尋找到方法,使用UIManager類可以辦到。例如在本例中,解決方法爲:

 UIManager.put("TextField.inactiveForeground", new Color(0, 0, 0));

更多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。

發佈了20 篇原創文章 · 獲贊 25 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章