为什么我的 Combobox 不能正确更新它的颜色?
Why doesn't my Combobox update its color properly?
我正在我的程序中实现暗模式,一切正常,除了组合框,它不想按我的意愿改变它的颜色。
(来源:bilder-upload.eu)
如您所见,组合框的“弹出窗口”可以很好地改变颜色,但组合框本身不会。 Combobox 的前景色也改变了,但背景没有。
我想,外观可能会导致问题。
在我的主要-class:
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
我在哪里更改为暗模式:
TeamInterface.userFilterComboBox.setBackground( darkBackgroundColor );
TeamInterface.userFilterComboBox.setForeground( fontColor );
SwingUtilities.updateComponentTreeUI( TeamInterface.userFilterComboBox );
我必须使用 updateComponentTreeUI 方法,否则“弹出窗口”也会保持白色。
如果我删除 main-class 中的外观和感觉,组合框看起来不错,正如您在这张图片中看到的那样,
(来源:bilder-upload.eu)
但我不想摆脱系统的外观,所以我尝试使用以下代码将组合框的 UI 手动编辑为金属:
userFilterComboBox.setUI( new MetalComboBoxUI() );
但是..结果很糟糕,即使理论上(至少我是这么想的)它看起来应该和没有外观和感觉的一样
(来源:bilder-upload.eu)
Combobox不仅仅是背景和前景的一个组件,而是一个复杂的组件。
example:JComboBox 组成为:
- 箭头按钮
- itme 列表
- 边框(并且有颜色)
- 所选项目
因此,对于更改,您可以在 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 有一个系统主题,用于在您的应用程序中创建个人时间,并且所有主题都是可个性化的。
This is the repo vincenzoapalazzo/material-ui-swing and atarw/material-ui-swing 是相同的存储库和相同的开发人员,因此,vincenzopalazzo/material-us-swing 是开发人员分支,包含更多修复和测试。
图书馆的一个例子是
.
Ps:我是MaterialTheming System的设计师。
我正在我的程序中实现暗模式,一切正常,除了组合框,它不想按我的意愿改变它的颜色。
(来源:bilder-upload.eu)
如您所见,组合框的“弹出窗口”可以很好地改变颜色,但组合框本身不会。 Combobox 的前景色也改变了,但背景没有。
我想,外观可能会导致问题。
在我的主要-class:
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
我在哪里更改为暗模式:
TeamInterface.userFilterComboBox.setBackground( darkBackgroundColor );
TeamInterface.userFilterComboBox.setForeground( fontColor );
SwingUtilities.updateComponentTreeUI( TeamInterface.userFilterComboBox );
我必须使用 updateComponentTreeUI 方法,否则“弹出窗口”也会保持白色。 如果我删除 main-class 中的外观和感觉,组合框看起来不错,正如您在这张图片中看到的那样,
(来源:bilder-upload.eu)
但我不想摆脱系统的外观,所以我尝试使用以下代码将组合框的 UI 手动编辑为金属:
userFilterComboBox.setUI( new MetalComboBoxUI() );
但是..结果很糟糕,即使理论上(至少我是这么想的)它看起来应该和没有外观和感觉的一样
(来源:bilder-upload.eu)
Combobox不仅仅是背景和前景的一个组件,而是一个复杂的组件。 example:JComboBox 组成为:
- 箭头按钮
- itme 列表
- 边框(并且有颜色)
- 所选项目
因此,对于更改,您可以在 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 有一个系统主题,用于在您的应用程序中创建个人时间,并且所有主题都是可个性化的。
This is the repo vincenzoapalazzo/material-ui-swing and atarw/material-ui-swing 是相同的存储库和相同的开发人员,因此,vincenzopalazzo/material-us-swing 是开发人员分支,包含更多修复和测试。
图书馆的一个例子是
Ps:我是MaterialTheming System的设计师。