2015-08-25 67 views
-3
JList list; 
list=new JList(); 
list.add("string"); 

我無法運行程序(出現錯誤)。錯誤說:當我將項目添加到JList時,爲什麼會出現錯誤?

The method add(Component) in the type `Container` is not applicable for the arguments (int) 

我嘗試使用JList<String>,但它仍然無法正常工作? 謝謝!

+0

名單VS JList的.. –

回答

2

您不能直接將數據添加到JList

改爲使用ListModel

DefaultListModel<String> listModel = new DefaultListModel<String>(); 
    list.setModel(listModel); 

    // Add elements to the Model 
    listModel.addElement("hello"); 
+0

什麼是列表模式?我試圖從各種谷歌搜索瞭解它,但我不明白。它與模型 - 視圖 - 控制有關,對嗎? –

1

做一個數組項目添加到:

arraylist string = new arraylist [size that you want]; 
arraylist.add (what u want to add in here); 
相關問題