我有一個名爲PlaceCategory的類,它有一個名稱和一個顏色。當我想創建一個新的PlaceCategory時,首先輸入它的名稱,然後選擇一種顏色。它們被保存爲字符串和顏色。當我完成後,我用這些設置創建一個新的對象,然後我想將它添加到我的JList,但它不工作,我得到這個錯誤「類型Container中的方法add(Component)不適用對於參數(PlaceCategory)」這裏是我的代碼無法添加到我的JList?
class NewCatLis implements ActionListener{
public void actionPerformed(ActionEvent ave){
String categoryName;
Color color = Color.BLACK;
categoryName = JOptionPane.showInputDialog(MapProgram.this, "Name on category");
color = JColorChooser.showDialog(MapProgram.this,"Chooser color", color);
PlaceCategory pc = new PlaceCategory(categoryName, color);
categoryList.add(pc);
}
}
這裏是我的JList
private JList<PlaceCategory> categoryList;
MapProgram(){
super("map");
PlaceCategory[] category = {new PlaceCategory("Tunnelbana", Color.GREEN)};
categoryList = new JList<PlaceCategory>(category);
categoryList.setVisibleRowCount(3);
categoryList.setFixedCellWidth(50);
east.add(new JScrollPane(categoryList));
categoryList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
,這裏是我的PlaceCategory類:
import java.awt.*;
public class PlaceCategory {
private String name;
public Color color;
public PlaceCategory(String name, Color color){
this.name = name;
this.color = color;
}
public String toString(){
return name;
}
public Color getColor() {
return color;
}
}
https://docs.oracle.com/javase/tutorial/uiswing/components/list.html#mutable –