簡體   English   中英

為什么我的 Combobox 不能正確更新顏色?

[英]Why doesn't my Combobox update its color properly?

我在我的程序中實現了一個暗模式,一切正常,除了 Combobox,它不想改變它的顏色。

1
(來源: bilder-upload.eu

如您所見,Combobox 的“彈出窗口”會很好地改變顏色,但 Combobox 本身不會。 Combobox 的前景色也會發生變化,但背景不會發生變化。

我想,外觀可能會導致問題。

在我的主要課程中:

UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );

我更改為暗模式的地方:

TeamInterface.userFilterComboBox.setBackground( darkBackgroundColor );
TeamInterface.userFilterComboBox.setForeground( fontColor );
SwingUtilities.updateComponentTreeUI( TeamInterface.userFilterComboBox );

我必須使用 updateComponentTreeUI-Method,否則“彈出窗口”也會保持白色。 如果我刪除主類中的外觀和感覺,combobox 看起來不錯,正如您在這張圖片中看到的那樣,

2
(來源: bilder-upload.eu

但我不想擺脫系統外觀,所以我嘗試使用以下代碼手動將 combobox 的 UI 編輯為金屬:

userFilterComboBox.setUI( new MetalComboBoxUI() );

但是..結果很糟糕,即使理論上(至少我認為)它應該看起來和沒有外觀和感覺一樣

3
(來源: bilder-upload.eu

Combobox 不是僅對背景和前景的組件,而是復雜組件。 一個例子:JComboBox 由以下組成:

  • 箭頭按鈕
  • 項目列表
  • 邊框(並且有顏色)
  • 選擇的項目

所以為了改變你可以在你的 UIManager 中添加的所有內容,所有常量或者你可以定義一個新的 UIComponent。

因此PersonalComboBoxUI可以執行以下操作:

/**
 * @contributor https://github.com/vincenzopalazzo
 */
public class PersonalComboBoxUI extends BasicComboBoxUI {

    public static ComponentUI createUI (JComponent c) {
        return new PersonalComboBoxUI ();
    }

    @Override
    public void installUI (JComponent c) {
        super.installUI (c);

        JComboBox<?> comboBox = (JComboBox<?>) c;
        comboBox.setBackground (UIManager.getColor ("ComboBox.background"));
        comboBox.setForeground (UIManager.getColor ("ComboBox.foreground"));
        comboBox.setBorder (UIManager.getBorder ("ComboBox.border"));
        comboBox.setLightWeightPopupEnabled (true);
    }

    @Override
    protected JButton createArrowButton () {
        Icon icon = UIManager.getIcon ("ComboBox.buttonIcon");
        JButton button;
        if (icon != null) {
            button = new JButton (icon);
        }
        else {
            button = new BasicArrowButton (SwingConstants.SOUTH);
        }
        button.setOpaque (true);
        button.setBackground (UIManager.getColor ("ComboBox.buttonBackground"));
        button.setBorder (BorderFactory.createLineBorder(Color.black));
        return button;
    }

    @Override
    protected ListCellRenderer createRenderer() {
        return new MaterialComboBoxRenderer();
    }
}

您還應該定義 PersonalComboBoxRenderer

/**
 * @contributor https://github.com/vincenzopalazzo
 */
    public class PersonalComboBoxRenderer extends BasicComboBoxRenderer {

        @Override
        public Component getListCellRendererComponent (JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JComponent component = (JComponent) super.getListCellRendererComponent (list, value, index, isSelected, cellHasFocus);

            component.setBorder (BorderFactory.createEmptyBorder (5, 5, 5, 5));
            component.setForeground (UIManager.getColor ("ComboBox.foreground"));
            component.setBackground (isSelected || cellHasFocus ?
                    UIManager.getColor("ComboBox.selectedInDropDownBackground") :
                    UIManager.getColor("ComboBox.background"));

            return component;
        }
    }

ps:在這種情況下,我使用 UIManager.put("ComboBox.background", COLOR) 在 JComponent 內部進行添加和分層。

因此,我想添加兩個信息,說明您是否在 UIManager 或 PersonalComboBoxUI 中使用個人顏色,顏色應使用此代碼定義

Color PINK_400 = new ColorUIResource (236, 64, 122);

因為當您 go 刪除外觀時,顏色無法刪除,但如果您使用ColorUIResource ,則應該正確刪除外觀。

最后,如果您不需要默認外觀,我建議您使用庫。

material-UI-swing有一個系統主題,用於在您的應用程序中創建個人計時,並且所有主題都是可個性化的。

這是 repo vincenzoapalazzo/material -ui-swingatarw/material-ui-swing是同一個存儲庫和同一個開發者,所以vincenzoapalazzo/material-us-swing是開發者分支,包含更多修復和測試。

圖書館的一個例子是

主題 .

Ps:我是MaterialTheming System的設計師。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM