JComboBox目前选择了项目背景和Windows L& F.

时间:2015-04-09 13:27:09

标签: java swing colors jcombobox windows-look-and-feel

我当时找不到其他方式:

UIManager.put("ComboBox.background",new Color(31,0,104));
UIManager.put("ComboBox.selectionBackground",new Color(31,0,104));

在JComboBox中设置当前所选项目的backgorund颜色。 不幸的是,这对于我不想要的窗口中的所有组合框都是一样的。

目前的工作原理如下: PICTURE

这就是我希望它表现的方式: PICTURE

我尝试创建自定义DefaultListCellRenderer并在那里调用setBackground,但它没有帮助。我也尝试重写BasicComboBoxUI,但我可能不会覆盖哪种方法来实现结果。

编辑: 根据建议,这是我的代码的一小部分,但它没有帮助

public void colorizeComboBox(){

     jComboBox1.setRenderer(new ColourListCellRenderer());
    jComboBox1.setUI(new BasicComboBoxUI(){
        @Override 
        protected JButton createArrowButton() {
            JButton b = this.createCustomArrowButton();
            b.setContentAreaFilled(false);
            b.setBackground(new Color(31,0,104));
            b.setBorder(BorderFactory.createEmptyBorder());
            return b;
         }


        public JButton createCustomArrowButton() {
              JButton b=new JButton(new ImageIcon(getClass().getResource("/server/Pictures/arrowdown.png")));
              b.setBorder(new EmptyBorder(0, 0, 0, 0));
              b.setOpaque(true);
              return b;
        }
    });

和自定义渲染器:

  public static  class ColourListCellRenderer extends DefaultListCellRenderer {


     @Override
    public Color getBackground() {
       return new Color(31,0,104);
     }


}

但现在改变的是当前所选项目的背景为白色。

好的,终于找到了解决方案:

 jComboBox1.setUI(new BasicComboBoxUI(){


        @Override
        public void paintCurrentValueBackground(Graphics g,Rectangle bounds,boolean hasFocus){

             Color t = g.getColor();
            if ( comboBox.isEnabled() )
                g.setColor(new Color(31,0,104));
            else
                g.setColor(DefaultLookup.getColor(comboBox, this,
                                         "ComboBox.disabledBackground", null));
            g.fillRect(bounds.x,bounds.y,bounds.width,bounds.height);
            g.setColor(t);
        }






    });
     jComboBox1.setFocusable(false);

我必须在BasicComboBoxUI中覆盖paintCurrentValueBackground(..),然后将此UI设置为组合框,但焦点仍然存在问题,并且将组合boxx绘制为灰色,因此我禁用了对此组合框的关注

2 个答案:

答案 0 :(得分:0)

覆盖DefaultListCellRenderer的getBackground和getListCellRendererComponent方法。

public class ColoredListCellRenderer extends DefaultListCellRenderer {

    private Color selectedColor;
    private Color backgroundColor;
    private Color lastColor;

    public ColoredListCellRenderer(Color backgroundColor, Color selectedColor) {
        this.selectedColor = selectedColor;
        this.backgroundColor = backgroundColor;
        lastColor = backgroundColor;
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        if (isSelected) {
            lastColor = selectedColor;
        } else {
            lastColor = backgroundColor;
        }
        c.setBackground(lastColor);
        return c;
    }

    @Override
    public Color getBackground() {
        return lastColor;
    }

}

答案 1 :(得分:0)

OP 的回答从问题变成了答案:

<块引用>

好的终于找到解决方案了:

 jComboBox1.setUI(new BasicComboBoxUI(){
       
        
        @Override
        public void paintCurrentValueBackground(Graphics g,Rectangle bounds,boolean hasFocus){
            
             Color t = g.getColor();
            if ( comboBox.isEnabled() )
                g.setColor(new Color(31,0,104));
            else
                g.setColor(DefaultLookup.getColor(comboBox, this,
                                         "ComboBox.disabledBackground", null));
            g.fillRect(bounds.x,bounds.y,bounds.width,bounds.height);
            g.setColor(t);
        }
        
    
        
        
        
        
    });
     jComboBox1.setFocusable(false);

我不得不在 BasicComboBoxUI 中覆盖paintCurrentValueBackground(..) ,然后将此 UI 设置为组合框,但焦点仍然存在问题并且 将组合框绘制为灰色,因此我禁用了对这个组合框的关注