2016-09-08 69 views
0

我遇到了JComboBox自定義ListCellRenderer的問題。我想創建一個組合框,讓它在4種顏色之間進行選擇。我希望組合框通過JPanel的「列表」以所需的背景顏色顯示顏色。我設法得到我想要的東西,唯一的問題是,當我點擊物品時,這種顏色變成灰色,不再顯示原來的顏色。 下面的代碼:JComboBox自定義ListCellRenderer

package custom_renderer_combobox; 

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Dimension; 

import javax.swing.JFrame; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.ListCellRenderer; 
import javax.swing.JComboBox; 

public class gui extends JFrame{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    private Color[] colors={Color.BLACK,Color.blue,Color.cyan,Color.GRAY,Color.green,Color.MAGENTA,Color.ORANGE,Color.RED}; 
    gui(){ 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
     setSize(new Dimension(400,400)); 
     getContentPane().setLayout(null); 

     JComboBox<JPanel> comboBox = new JComboBox<JPanel>(); 
     comboBox.setRenderer(new myRenderer()); 
     comboBox.setBounds(36, 59, 211, 20); 
     getContentPane().add(comboBox); 
     for(int i=0;i<colors.length;i++){ 
      JPanel temp=new JPanel(); 
      temp.setOpaque(true); 
      temp.setBackground(colors[i]); 
      comboBox.addItem(temp); 
     } 
     comboBox.repaint(); 
    } 

    class myRenderer implements ListCellRenderer{ 


     @Override 
     public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
      return ((JPanel) value); 
     } 

    } 
} 

這裏是一些圖片:

image

我認爲這個問題是在自定義渲染器,可有人告訴我,我將不得不這樣做?

回答

2

讓它有點快,你可以創建自己的JPanel,將採取背景色的構造,並且將阻止變化的背景:

public class ColorPanel extends JPanel { 

    public ColorPanel(Color color) { 
     super.setBackground(color); 
    } 

    @Override 
    public void setBackground(Color bg) { 
     //blocked changing 
    } 

} 

,並在你的代碼,你必須改變

 JPanel temp = new ColorPanel(colors[i]); 
     temp.setOpaque(true); 
     comboBox.addItem(temp); 
+0

準確地說,我想要的行爲。非常感謝!!!!! – z10h22

2

下面是一個例子,如何使用的渲染和組合框

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 

import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.WindowConstants; 
import javax.swing.plaf.basic.BasicComboBoxRenderer; 

/** 
* <code>ColorTest</code>. 
*/ 
public class ColorTest { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new ColorTest(); 
      } 
     }); 
    } 
    /** 
    * 
    */ 
    public ColorTest() { 
     JFrame frm = new JFrame("Test"); 
     final JComboBox<ColorData> cmb = new JComboBox<>(ColorData.values()); 
     cmb.setRenderer(new ColorRenderer()); 
     final JPanel p = new JPanel(); 
     p.setOpaque(true); 
     p.setPreferredSize(new Dimension(200, 100)); 
     ActionListener l = new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       ColorData cd = (ColorData) cmb.getSelectedItem(); 
       if (cd != null) { 
        p.setBackground(cd.getColor()); 
       } 
      } 
     }; 
     cmb.addActionListener(l); 
     l.actionPerformed(null); // update current background 
     frm.add(cmb, BorderLayout.NORTH); 
     frm.add(p); 
     frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     frm.pack(); 
     frm.setLocationRelativeTo(null); 
     frm.setVisible(true); 
    } 

    private static class ColorRenderer extends BasicComboBoxRenderer { 
     @Override 
     public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
      super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
      if (value instanceof ColorData) { 
       BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); 
       Graphics g = img.createGraphics(); 
       g.setColor(((ColorData) value).getColor()); 
       g.fillRect(0, 0, 15, 15); 
       g.dispose(); 
       setIcon(new ImageIcon(img)); 
      } 
      return this; 
     } 
    } 

    private static enum ColorData { 
     BLACK("Black", Color.BLACK), 
     BLUE("Blue", Color.BLUE), 
     GREEN("Green", Color.GREEN), 
     RED("Red", Color.RED); 
     private String name; 

     private Color color; 

     private ColorData(String aName, Color aColor) { 
      name = aName; 
      color = aColor; 
     } 

     public String getName() { 
      return name; 
     } 

     public Color getColor() { 
      return color; 
     } 

     @Override 
     public String toString() { 
      return getName(); 
     } 
    } 
} 

要添加一些新的顏色,您需要將相應的常量添加到ColorData枚舉。