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

我正在我的程序中实现深色模式,一切正常,除了组合框之外,它不想按照我的意愿更改其颜色。

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

正如您所看到的,组合框的“弹出窗口”很好地改变了颜色,但组合框本身却没有。组合框的前景色也发生变化,但背景颜色不变。


我想,外观和感觉可能会导致问题。


在我的主课中:


UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );

我更改为深色模式的地方:


TeamInterface.userFilterComboBox.setBackground( darkBackgroundColor );

TeamInterface.userFilterComboBox.setForeground( fontColor );

SwingUtilities.updateComponentTreeUI( TeamInterface.userFilterComboBox );

我必须使用 updateComponentTreeUI-Method,因为否则“弹出窗口”也保持白色。如果我删除主类中的外观和感觉,组合框看起来不错,正如您在这张图片中看到的,

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

但我不想摆脱系统的外观和感觉,所以我尝试使用以下代码手动将组合框的 UI 编辑为金属:

userFilterComboBox.setUI( new MetalComboBoxUI() );

但是..结果很糟糕,即使理论上(至少我是这么认为的)它应该看起来和没有外观和感觉一样

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

蓝山帝景
浏览 50回答 1
1回答

慕桂英4014372

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是开发人员分支,包含更多修复和测试。图书馆的一个例子是
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java