【问题标题】:Pick Color with JComboBox Java Swing使用 JComboBox Java Swing 选择颜色
【发布时间】:2013-09-20 17:25:08
【问题描述】:

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

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 字段中?

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

我已经看过几个类似的问题,但我无法弄清楚代码的哪一部分是在选择完成时处理JCombobox的颜色...

【问题讨论】:

  • 请对代码、输入/输出和结构化文档(如 HTML 或 XML)使用代码格式。为此,请选择示例并单击消息发布/编辑表单上方的{} 按钮。
  • 我使用 CODE 按钮在我的问题中标记代码...下次我将尝试使用 {} ;) 不便之处,敬请原谅。

标签: java swing colors jcombobox


【解决方案1】:

试试这个,应该可以的。您必须覆盖 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;  
     }  
}

【讨论】:

  • 干杯 m8!非常感激!我刚刚将 setText(" "); 更改为 setText(""); 并将 setPreferredSize(new Dimension(50,20)); 行添加到 getListCellRendererComponent 方法以完全实现我的目标。会投票给你,但我需要 15 声望 :) 再次感谢 m8!
【解决方案2】:

ComboBox 使用 equals 并且所有字符串都相等。 定义颜色名称

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

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

【讨论】:

  • 我已按照您的指示将代码更改为:toolbarPanel.add(fillButton); String[] test = {"white", "red" , "blue" , "yellow" , "green" , "gray"}; JComboBox colorBox = new JComboBox(test); 并在渲染器中更改为:setText(" "); switch (index) { case 0: setBackground(Color.white); break;,但当我进行选择时,JComboBox 中仍显示灰色,无论我有什么颜色已选择...感谢您抽出时间 m8!赞赏!
  • 我会使用安全的 Color colors[] = {Color.BLACK, Color.BLUE, Color.GREEN, Color.RED, Color.WHITE, Color.YELLOW};
  • @mKorbel 同意颜色更好:)
  • 而不是使用 switch(index) 处理值并从值设置颜色。
  • 谢谢大家的回答,干杯! :) Alhugone 的代码完全符合我的目标。这些天我会用 ListCellRenderers 做一些实验;)
【解决方案3】:

为 index == -1 添加 case 并将单元格渲染器的背景颜色设置为最近的用户选择。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 2023-03-12
    • 2016-11-07
    • 1970-01-01
    • 2022-01-22
    • 2014-05-07
    • 1970-01-01
    相关资源
    最近更新 更多