繁体   English   中英

JComboBox 在每个项目中都有不同的颜色失败

[英]JComboBox having different color in every item failed

  • 目的:在每个项目中拥有一个具有不同背景 colors 和文本的 JComboBox。
  • 我的问题:背景颜色没有改变,文本不是我在setText中设置的,在System.out.println中已经正确显示。 getSelectedIndex() 效果很好。

截图: http://i.stack.imgur.com/EgfZs.png

以下是我消化并尝试错误后的代码 Dr.Google 显示:

public class ColorCode{
   private Color color;
   private String alias;
   ...
}
public class ElectronicColorCode extends JFrame implements ActionListener{
   private JComboBox[] selections = new JComboBox[4];
   ...
   public ElectronicColorCode(){
      for(int i=0; i<selections.length; i++){
         selections[i] = new JComboBox();
         for(int j=0; j<tolColorSets.length; j++)
            selections[i].addItem(new ComboBoxRenderer(colorSets[j]));
      }
      selections[i].addActionListener(this);
      ...
   }
}
class ComboBoxRenderer extends JLabel implements ListCellRenderer{
   private ColorCode colorCode;

   public ComboBoxRenderer(ColorCode colorCode){
      super();
      this.colorCode = colorCode;
      setBackground(colorCode.getColor());
      setText(colorCode.getAlias());
      System.out.println(colorCode.getAlias());
   }

   public Component getListCellRendererComponent(JList list, Object obj, int row, boolean isSelected, boolean hasFocus){
      return this;
   }
}

您不会将渲染器添加为组合框的项目。 渲染器用于渲染存储在 model 中的 Object。 如果需要,可以将自定义 object 添加到 model 中,其中包含要在渲染器中显示的文本和背景颜色。

这是一个简单的例子,展示了如何做到这一点。 您显然需要自定义代码来存储和呈现背景颜色而不是 id。

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class ComboBoxItem2 extends JFrame implements ActionListener
{
    public ComboBoxItem2()
    {
        Vector model = new Vector();
        model.addElement( new Item(1, "car" ) );
        model.addElement( new Item(2, "plane" ) );
        model.addElement( new Item(4, "boat" ) );
        model.addElement( new Item(3, "train" ) );

        JComboBox comboBox = new JComboBox( model );
        comboBox.setRenderer( new ItemRenderer() );
        comboBox.addActionListener( this );
        getContentPane().add(comboBox, BorderLayout.SOUTH );
    }

    public void actionPerformed(ActionEvent e)
    {
        JComboBox comboBox = (JComboBox)e.getSource();
        Item item = (Item)comboBox.getSelectedItem();
        System.out.println( item.getId() + " : " + item.getDescription() );
    }

    class ItemRenderer extends BasicComboBoxRenderer
    {
        public Component getListCellRendererComponent(
            JList list, Object value, int index,
            boolean isSelected, boolean cellHasFocus)
        {
            super.getListCellRendererComponent(list, value, index,
                isSelected, cellHasFocus);

            Item item = (Item)value;
            setText( item.getDescription().toUpperCase() );

            return this;
        }
    }

    class Item
    {
        private int id;
        private String description;

        public Item(int id, String description)
        {
            this.id = id;
            this.description = description;
        }

        public int getId()
        {
            return id;
        }

        public String getDescription()
        {
            return description;
        }

        public String toString()
        {
            return description;
        }
    }

    public static void main(String[] args)
    {
        JFrame frame = new ComboBoxItem2();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
     }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM