2013-09-16 42 views
4

我有一个JComboBox,我希望用户选择颜色。 JComboBox只显示颜色,没有任何文字。我已经想出了这个解决方案。请告诉我这是好还是应该避免,为什么。我是Swing和Java的新手,因此请耐心等待:)用JComboBox Java Swing挑选颜色

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; 
     } 
    } 
} 

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

我尝试了一些解决方案,但结果是,当用户在挑选JComboBox的颜色选择总是变成灰色......

我通过几个类似的问题看,但我只是想不通的代码部分正在处理JComboBox在选择完成后颜色的变化...

+0

请使用代码格式化代码,输入/输出结构化文档如HTML或XML。为此,请选择样本并单击邮件发布/编辑表单上方的“{}”按钮。 –

+0

我使用CODE按钮来标记我的问题中的代码...我将在下次{}时尝试;}对于给您带来的不便,我们深表歉意。 –

回答

3

试试这个,应该可以工作。你必须重载的setBackground ......因为,内部机制使用默认的颜色从目前看&感觉:

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:

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; 
    } 
} 
+0

干杯m8!非常感激!我刚刚改变了'setText(“”);'setText(“”);'并且增加了'setPreferredSize(new Dimension(50,20));'getListCellRendererComponent方法来完全实现我想要的目标。会投你,但我需要15声望:)再次感谢m8! –

2

ComboBox使用等于和所有你的字符串是相等的。 定义颜色名称

String[] test = {"red", "green" , "blue" ...}; 

但是在渲染调用setText(" ");

+0

我按照指示更改了代码: 'toolbarPanel.add(fillButton); \t \t \t \t String [] test = {“white”,“red”,“blue”,“yellow”,“green”,“gray”}; \t \t JComboBox的颜色框=新JComboBox的(测试);' 并在渲染器: '的setText(”“); \t switch(index){ \t case 0:setBackground(Color.white); \t break;' 但仍然当我做出选择灰色颜色显示在JComboBox,无论我选择的颜色... 谢谢你的时间m8!不胜感激! –

+2

我会安全使用颜色[] = {Color.BLACK,Color.BLUE,Color.GREEN,Color.RED,Color.WHITE,Color.YELLOW}; – mKorbel

+1

@mKorbel同意颜色更好:) – StanislavL

0

添加的情况下,对指数== -1和格渲染器的背景颜色设置为最近的用户选择。