简体   繁体   English

为什么我的 Combobox 不能正确更新颜色?

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

I am implementing a dark mode into my program and everything works just fine, except a Combobox, which doesn't want to change its color as I want.我在我的程序中实现了一个暗模式,一切正常,除了 Combobox,它不想改变它的颜色。

1
(source: bilder-upload.eu ) (来源: bilder-upload.eu

So as you can see, the "popup" of the Combobox changes the color just fine, but the Combobox itself doesn't.如您所见,Combobox 的“弹出窗口”会很好地改变颜色,但 Combobox 本身不会。 Also the Foreground color of the Combobox changes, but the background not. Combobox 的前景色也会发生变化,但背景不会发生变化。

I guess, the Look and Feel might cause the issue.我想,外观可能会导致问题。

In my main-class:在我的主要课程中:

UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );

Where I change to Darkmode:我更改为暗模式的地方:

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

I have to use the updateComponentTreeUI-Method, because otherwise the "popup" also remains white.我必须使用 updateComponentTreeUI-Method,否则“弹出窗口”也会保持白色。 If I remove the look and feel in my main-class, the combobox looks good,as you can see in this picture,如果我删除主类中的外观和感觉,combobox 看起来不错,正如您在这张图片中看到的那样,

2
(source: bilder-upload.eu ) (来源: bilder-upload.eu

but I doesn't want to get rid of the system look and feel, so I tried to manually edit the UI of the combobox to metal with this code:但我不想摆脱系统外观,所以我尝试使用以下代码手动将 combobox 的 UI 编辑为金属:

userFilterComboBox.setUI( new MetalComboBoxUI() );

but.. the result is just awful, even thoe theoretically (at leats thats what I think) it should look the same as without look and feel但是..结果很糟糕,即使理论上(至少我认为)它应该看起来和没有外观和感觉一样

3
(source: bilder-upload.eu ) (来源: bilder-upload.eu

The Combobox not is a component only to the background and the foreground but is the complex component. Combobox 不是仅对背景和前景的组件,而是复杂组件。 An example:JComboBox is composed to:一个例子:JComboBox 由以下组成:

  • ArrowButton箭头按钮
  • List of itme项目列表
  • Border (and it have a color)边框(并且有颜色)
  • the item selected选择的项目

So for change all you can add inside your UIManager, all constant or you can define a new UIComponent.所以为了改变你可以在你的 UIManager 中添加的所有内容,所有常量或者你可以定义一个新的 UIComponent。

So an PersonalComboBoxUI can the following:因此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();
    }
}

You should be defined also the PersonalComboBoxRenderer您还应该定义 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: in this case, I'm using the UIManager.put("ComboBox.background", COLOR) for add and stratification inside the JComponent. ps:在这种情况下,我使用 UIManager.put("ComboBox.background", COLOR) 在 JComponent 内部进行添加和分层。

So I want to add two information regarding if you using the personal color inside the UIManager or the PersonalComboBoxUI the color should be defined with this code因此,我想添加两个信息,说明您是否在 UIManager 或 PersonalComboBoxUI 中使用个人颜色,颜色应使用此代码定义

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

because when you go to remove look and feel the color couldn't remove but if you used ColorUIResource the look and feel should be removed correctly.因为当您 go 删除外观时,颜色无法删除,但如果您使用ColorUIResource ,则应该正确删除外观。

To finish, if you do not have needed the default look and feel, I want to suggest you use a library.最后,如果您不需要默认外观,我建议您使用库。

The material-UI-swing has a system theming for creating the personal timing in your app and the all theme is personalizable. material-UI-swing有一个系统主题,用于在您的应用程序中创建个人计时,并且所有主题都是可个性化的。

This is the repo vincenzoapalazzo/material-ui-swing and atarw/material-ui-swing are the same repository and the same developer so, the vincenzopalazzo/material-us-swing is the developer branch, an contains more fix and test.这是 repo vincenzoapalazzo/material -ui-swingatarw/material-ui-swing是同一个存储库和同一个开发者,所以vincenzoapalazzo/material-us-swing是开发者分支,包含更多修复和测试。

An example of the library is图书馆的一个例子是

主题 . .

Ps: I am the designer of the MaterialTheming System . Ps:我是MaterialTheming System的设计师。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM