使用JComboBox Java Swing选择颜色 [英] Pick Color with JComboBox Java Swing

查看:1140
本文介绍了使用JComboBox Java Swing选择颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JComboBox,我想让用户选择颜色。 JComboBox只显示颜色,没有任何文本。我想出了这个解决方案。请告诉我这是好还是应该避免,为什么。我对Swing和Java一般是新的,所以请耐心等待)。

I have a JComboBox where I want for user to select color. JComboBox is displaying just the colors, without any text. I've come up with this solution. Please advise me if this is good or it should be avoided and why. I am new to Swing and Java in general so please be patient :)

public class ToolBar{
    private MainFrame mainFrame;

    public ToolBar (MainFrame mainFrame) {
        this.mainFrame = mainFrame;
    }

    public JPanel getToolBar(){

        JPanel toolbarPanel = new JPanel(new FlowLayout(FlowLayout.LEADING,2,2));
        toolbarPanel.setPreferredSize(new Dimension(mainFrame.getScreenWidth(),60));
        toolbarPanel.setBorder(BorderFactory.createLineBorder(Color.gray));

        JButton fillButton = new JButton("Fill: ");
        fillButton.setPreferredSize(new Dimension(60,20));
        //fillButton.setBackground(Color.red);
        toolbarPanel.add(fillButton);

        String[] test = {" ", " " , " " , " " , " " , " "};
        JComboBox colorBox = new JComboBox(test);
        colorBox.setMaximumRowCount(5);
        colorBox.setPreferredSize(new Dimension(50,20));
        colorBox.setRenderer(new MyCellRenderer());
        toolbarPanel.add(colorBox);

        return toolbarPanel;
    }
    class MyCellRenderer extends JLabel implements ListCellRenderer {  
         public MyCellRenderer() {  
             setOpaque(true);  
         }  
         public Component getListCellRendererComponent(  
             JList list,  
             Object value,  
             int index,  
             boolean isSelected,  
             boolean cellHasFocus)  
         {  
             setText(value.toString()); 
             switch (index) {
                case 0:  setBackground(Color.white);
                break;
                case 1:  setBackground(Color.red);
                break;
                case 2:  setBackground(Color.blue);
                break;
                case 3:  setBackground(Color.yellow);
                break;
                case 4:  setBackground(Color.green);
                break;
                case 5:  setBackground(Color.gray);
                break;
             }
             return this;  
         }  
    }
}

它使用不同的颜色在JComboBox中显示空选择元素。问题是,当用户选择颜色时,JComboBox中的选择颜色不会改变。我应该添加哪些代码行和位置,以便当用户从列表中选择颜色显示在JComboBox字段中的颜色?

This works ok. It is displaying empty selection elements in JComboBox with different colors. Problem is that when user selects color, color of selection in JComboBox does not change. Which lines of code should I add and where so that when user selects the color from a list that color is displayed in JComboBox field?

我尝试了一些解决方案,但结果是,当用户选择颜色选择在JComboBox总是更改为灰色...

I tried some solutions but result was that when user picks color selection in JComboBox always changes to gray...

我已经查看了几个类似的问题,但我只是无法弄清楚哪部分代码处理改变颜色的JComboBox当选择已经完成...

I've looked through several similar questions but I just can't figure out which part of code is dealing with changing of color of JComboBox when the selection has been done...

推荐答案

尝试这个,应该工作。你必须重写setBackground ...因为,内部机制使用当前的外观和感觉的默认颜色:

Try this, should work. You have to override setBackground... because, internal mechanism uses default colors from current Look&Feel:

Color[] colors={Color.white,Color.red,Color.blue,Color.green};
JComboBox colorBox = new JComboBox(colors);
colorBox.setMaximumRowCount(5);
colorBox.setPreferredSize(new Dimension(50,20));
colorBox.setRenderer(new MyCellRenderer());

和ListCellRender:

And ListCellRender:

class MyCellRenderer extends JButton implements ListCellRenderer {  
     public MyCellRenderer() {  
         setOpaque(true); 

     }
     boolean b=false;
    @Override
    public void setBackground(Color bg) {
        // TODO Auto-generated method stub
         if(!b)
         {
             return;
         }

        super.setBackground(bg);
    }
     public Component getListCellRendererComponent(  
         JList list,  
         Object value,  
         int index,  

         boolean isSelected,  
         boolean cellHasFocus)  
     {  

         b=true;
         setText(" ");           
         setBackground((Color)value);        
         b=false;
         return this;  
     }  
}

这篇关于使用JComboBox Java Swing选择颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆