gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:28:30 25 4
gpt4 key购买 nike

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

1
(来源:bilder-upload.eu)

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

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

在我的主课中:

UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );

我在哪里更改为黑暗模式:

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

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

2
(来源:bilder-upload.eu)

但我不想摆脱系统的外观和感觉,所以我尝试使用以下代码手动将组合框的 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);

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

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

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

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

该库的一个示例是

OcenicTheme .

Ps:我是MaterialTheming System的设计师。

关于java - 为什么我的组合框无法正确更新其颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58299390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com