为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我的组合框无法正确更新其颜色?

为什么我的组合框无法正确更新其颜色?

蓝山帝景 2023-10-12 16:59:01
我正在我的程序中实现深色模式,一切正常,除了组合框之外,它不想按照我的意愿更改其颜色。正如您所看到的,组合框的“弹出窗口”很好地改变了颜色,但组合框本身却没有。组合框的前景色也发生变化,但背景颜色不变。我想,外观和感觉可能会导致问题。在我的主课中:UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );我更改为深色模式的地方:TeamInterface.userFilterComboBox.setBackground( darkBackgroundColor );TeamInterface.userFilterComboBox.setForeground( fontColor );SwingUtilities.updateComponentTreeUI( TeamInterface.userFilterComboBox );我必须使用 updateComponentTreeUI-Method,因为否则“弹出窗口”也保持白色。如果我删除主类中的外观和感觉,组合框看起来不错,正如您在这张图片中看到的,但我不想摆脱系统的外观和感觉,所以我尝试使用以下代码手动将组合框的 UI 编辑为金属:userFilterComboBox.setUI( new MetalComboBoxUI() );但是..结果很糟糕,即使理论上(至少我是这么认为的)它应该看起来和没有外观和感觉一样
查看完整描述

1 回答

?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

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);

因为当您去删除外观和感觉时,颜色无法删除,但如果您使用ColorUIResource,则应该正确删除外观和感觉。


最后,如果您不需要默认的外观,我建议您使用一个库。


Material -UI-swing有一个系统主题,用于在您的应用程序中创建个人计时,并且所有主题都是可个性化的。


这是仓库vincenzoapalazzo/material-ui-swing和atarw/material-ui-swing是相同的存储库和相同的开发人员,因此 vincenzopalazzo /material-us-swing是开发人员分支,包含更多修复和测试。


图书馆的一个例子是

https://img1.sycdn.imooc.com/6527b5bf00017a9a06520384.jpg



查看完整回答
反对 回复 2023-10-12
  • 1 回答
  • 0 关注
  • 47 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信