簡體   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