JComboBox 更改背景颜色

huangapple go评论83阅读模式
英文:

JComboBox change background color

问题

所以,我创建了一个靠近彼此的组合框(comboBox)和按钮(button)。
目标是更改组合框的背景,使其与JButton中的背景颜色相同。我为两者应用了相同的颜色,但结果是这样的。在不活动时,下拉列表和下箭头按钮的颜色都是灰色的。

当我点击组合框时,下箭头按钮的颜色和列表本身的背景颜色会更改为所需的颜色。顶部的固定元素变得更加深灰色,但仍然不是预期的颜色。

这是一个完全标准的组合框。下面是我尝试配置的内容。

import javax.swing.*;
import java.awt.*;

import static handler.Commands.COMMANDS_ARRAY;
import static java.awt.Color.WHITE;

public class MessageComboBox extends JComboBox {

    Dimension messageComboBoxDimension = new Dimension(242, 32);

    public MessageComboBox() {
        setBackground(new Color(71, 81, 93));
        getEditor().getEditorComponent().setBackground(new Color(71, 81, 93));
        ((JTextField) this.getEditor().getEditorComponent()).setOpaque(true);
        setModel(new DefaultComboBoxModel<>(COMMANDS_ARRAY));
        setVisible(true);
        setFocusable(false);
        setPreferredSize(messageComboBoxDimension);
        setRenderer(new DefaultListCellRenderer() {
            @Override
            public void paint(Graphics g) {
                setBackground(new Color(71, 81, 93));
                setForeground(WHITE);
                super.paint(g);
            }
        });
    }
}

有关如何使JComboBox的下箭头按钮和固定下拉元素的背景与JButton中的相同,即使没有焦点,是否有任何建议?由于某种原因,它是灰色的。

这与外观(LookAndFeel)有关吗?
__________________________________________________________________________________

更新:
问题已在下一个主题中解决
LookAndFeel blocking JComboBox background change?
__________________________________________________________________________________

英文:

So, I created comboBox and button near each other.
The goal is to change the background of the combo box and make it the same as in the JButton. I applied the same color for both, but this is what I get. When inactive, the color of the drop-down list and the down button is kind of gray.

JComboBox 更改背景颜色

When I click on the combo box, the color of the down button and the background of the list itself change to the desired one. The top fixed element became more dark gray, but it's steel with not expected color.

JComboBox 更改背景颜色

This is an absolutely standard combo box. Here is what I am tried to configure.

import javax.swing.*;
import java.awt.*;

import static handler.Commands.COMMANDS_ARRAY;
import static java.awt.Color.WHITE;

public class MessageComboBox extends JComboBox {

    Dimension messageComboBoxDimension = new Dimension(242, 32);

    public MessageComboBox() {
        setBackground(new Color(71, 81, 93));
        getEditor().getEditorComponent().setBackground(new Color(71, 81, 93));
        ((JTextField) this.getEditor().getEditorComponent()).setOpaque(true);
        setModel(new DefaultComboBoxModel<>(COMMANDS_ARRAY));
        setVisible(true);
        setFocusable(false);
        setPreferredSize(messageComboBoxDimension);
        setRenderer(new DefaultListCellRenderer() {
            @Override
            public void paint(Graphics g) {
                setBackground(new Color(71, 81, 93));
                setForeground(WHITE);
                super.paint(g);
            }
        });
    }
}

Any suggestions on how to make the background for JComboBox down button and fixed drop-down element the same as in JButton even when is't not focused? For some reason it is gray.

Is this related to LookAndFeel?
__________________________________________________________________________________

Update:
Issue resolved in the next topic
LookAndFeel blocking JComboBox background change?
__________________________________________________________________________________

答案1

得分: 0

尝试一下这个:

yourCombo.getEditor().getEditorComponent().setBackGround(new Color(71, 81, 93))
对我有效无论是活动还是非活动状态

JComboBox 更改背景颜色

英文:

try this

yourCombo.getEditor().getEditorComponent().setBackGround(new Color(71, 81, 93))

works for me, active or inactive
JComboBox 更改背景颜色

huangapple
  • 本文由 发表于 2020年10月26日 02:19:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64527194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定