【问题标题】:Swing: Nimbus L&F disabled combobox background-colorSwing:Nimbus L&F 禁用组合框背景颜色
【发布时间】:2013-01-22 11:51:23
【问题描述】:

我已经搜索了几天的答案并阅读了很多关于 LookAndFeels 和不透明问题的信息,但找不到解决方案。我正在使用 NimbusLookAndFeel 开发一个带有 Swing 的 Java 应用程序。我对整体的外观很满意,但还是想修改一个视图的东西。现在我被卡住了,因为不知何故我无法设置禁用的 JCombobox 的背景颜色 (combobox.setEnabled(false);)

我已经用 UIManager.put(..) + 很多其他的东西尝试了无数不同的属性。

如果我使用另一个 L&F 类似这样的方法:

combobox.setRenderer(new DefaultListCellRenderer() {
            @Override
            public void paint(Graphics g) {
                setBackground(Color.WHITE);
                setForeground(Color.BLACK);
                super.paint(g);
            }               
});

对如何使用 Nimbus 执行此操作有任何建议吗?

【问题讨论】:

  • 不相关:a) 不要覆盖paint,而是覆盖paintComponent b) 只覆盖paintComponent 以进行自定义绘画c) 永远不会在paint方法中更改组件状态.也就是说:Nimbus 很少尊重其设置器配置的颜色属性。相反,提供一个自定义(每个组件)皮肤属性,如 f.i. 所述。在 Swing 圣经的Nimbus related chapter 和其中引用的文章中。

标签: swing look-and-feel nimbus


【解决方案1】:

我经常使用 Nimbus 默认设置。修改大部分组件都没问题,但是我不能用它来改变任何禁用组件的背景。

我最终写了一个这样的自定义 ListCellRenderer

public class DisabledListCellRenderer extends DefaultListCellRenderer {
    private static final long serialVersionUID = 1L;
    private JComponent component;

    public DisabledListCellRenderer(JComponent component) {
        this.component = component;
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.setColor(SwingHelper.disabledBackgroundColor);
        g.fillRect(0, 0, component.getSize().width, component.getSize().height);
        super.paintComponent(g);
    }
}

这终于奏效了,但我不确定这是否是一个好的解决方案

【讨论】:

    猜你喜欢
    • 2011-07-26
    • 1970-01-01
    • 2012-08-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 2019-05-15
    相关资源
    最近更新 更多