早上好,請,你能介意幫我確定爲什麼這ListCellRenderer類不設置在組合框單元的圖像圖標: 這裏的ListCellRenderer類:的JComboBox文字和ImageIcon的
class MyComboRendere implements ListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = new JLabel();
label.setOpaque(true);
label.setText(value.toString());
label.setIcon(new ImageIcon("/pics/Color-icon.png"));
if (isSelected)
if (index == 0)
label.setBackground(Color.RED);
else if (index == 1)
label.setBackground(Color.GREEN);
else
label.setBackground(Color.BLUE);
return label;
}
}
,這是一個方法來設置組合框:
public void setComboColor(){
Vector<String> colors=new Vector<>();
comboPanel=new JPanel(new BorderLayout());
colors.add("RED");
colors.add("GREEN");
colors.add("BLUE");
colorCombo=new JComboBox(colors);
colorCombo.setRenderer(new MyComboRendere());
comboPanel.add(colorCombo,BorderLayout.BEFORE_FIRST_LINE);
}
重耦合,在現實生活中沒有這樣做。不要爲此使用'索引'... – Mordechai 2013-02-22 09:28:26