0
- 目的:有一個JComboBox,有不同的背景顏色,並在每個項目的文本 。
- 我的問題:背景色不改變,並且文本不是我在setText中設置的,它已在System.out.println中正確顯示。 getSelectedIndex()運行良好。
捕獲:http://i.stack.imgur.com/EgfZs.png具有不同的顏色中的每一項的JComboBox失敗
以下是我的代碼已經消化,並嘗試和錯誤是什麼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;
}
}
我以前試過這個版本,jvm向我抱怨Object值不能轉換成Item。 – 2011-06-14 05:57:07
@Asakura凱塔爲我工作,@camickr +1 – mKorbel 2011-06-14 07:27:49
...這是由我的程序中的錯誤引起的...感謝您的指導 – 2011-06-14 08:38:02