在鼠标悬停时如何更改jcomboxjava单元格的颜色?

lymgl2op  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(416)

当我将鼠标移到组合框上的单元格上时,我正在尝试更改颜色。我是使用鼠标事件还是仅此单元格渲染器?
我用什么方法改变颜色 JComboBox 鼠标悬停?
这是我的密码:

jComboBox2.setRenderer(new DefaultListCellRenderer() {
    public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus) {
        Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        if (isSelected){
            c.setBackground(new Color(242,242,242));
        }
        if(index == 0){
            c.setForeground(new Color(242,242,242));
            c.setBackground(new Color(26,35,48));
        }
        if(index == 1){
            c.setForeground(new Color(101,150,226));
            c.setBackground(new Color(26,35,48));
        }
        if(index == 2){
            c.setForeground(new Color(77,105,205));
            c.setBackground(new Color(26,35,48));
        }
        if(index == 3){
            c.setForeground(new Color(137,71,255));
            c.setBackground(new Color(26,35,48));
        }
        if(index == 4){
            c.setForeground(new Color(212,43,230));
            c.setBackground(new Color(26,35,48));
        }
        if(index == 5){
            c.setForeground(new Color(235,75,75));
            c.setBackground(new Color(26,35,48));
        }
        else {

        }
        return c;
    }
});

r6hnlfcb

r6hnlfcb1#

问题:它还应该更改组合框的前景色。jCombox2.setforeground(新颜色(21121121121));如果(isselected){}不起作用,则在内部
放置if(isselected){c.setforeground(new color(21121121121121));}在else语句的末尾,以便我们可以覆盖颜色。。

70gysomp

70gysomp2#

我找到了答案
如果要改变悬停时的颜色,我只需要嵌套
这样地

if(index == 0){
    c.setForeground(new Color(242,242,242));
    c.setBackground(new Color(26,35,48));
    if (isSelected) {
        c.setBackground(new Color(49,62,80));
    }
}

组合框图像

相关问题