Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在JScrollPane中显示项目列表?_Java_Swing_List_Jscrollpane_Jlist - Fatal编程技术网

Java 如何在JScrollPane中显示项目列表?

Java 如何在JScrollPane中显示项目列表?,java,swing,list,jscrollpane,jlist,Java,Swing,List,Jscrollpane,Jlist,首先,我是一名编程初学者,我不太懂Java/编程术语,所以如果您能帮助我,请尝试用更简单的词解释 我试图在JScrollPane中显示单词列表。此列表由类WordList表示。JScrollPane位于另一个名为WordFinder的类中 在WordFinder中,代码类似于: WordList words = new WordList(); // (other GUI code in between) JScrollPane scrollPane = new JScrollPane(DON

首先,我是一名编程初学者,我不太懂Java/编程术语,所以如果您能帮助我,请尝试用更简单的词解释

我试图在JScrollPane中显示单词列表。此列表由类WordList表示。JScrollPane位于另一个名为WordFinder的类中

在WordFinder中,代码类似于:

WordList words = new WordList();

// (other GUI code in between)

JScrollPane scrollPane = new JScrollPane(DONT_KNOW);
    // you have list of words - wordList
    List<Word> wordList= new ArrayList<Word>();

    // create list model for JList
    DefaultListModel<Word> model = new DefaultListModel<Word>();
    // add all words from wordList to model
    for(Word word : wordList){
        model.addElement(word);
    }

    // create JList with model - model
    JList<Word> list = new JList<Word>(model);

   // create scroll pane for scrolling JList
    JScrollPane scrollPane = new JScrollPane(list);
public class WordList extends JList<Object> {....}
如果我把“单词”放在不知道的地方,我会得到一个警告,说“构造函数JScrollPane(单词列表)未定义”。我可以理解,JScrollPane不应该接受这个奇怪的随机类。那我该放什么呢?我知道可以将JList放在JScrollPane中,但是如何将WordList类转换为JList(或者JScrollPane可以接受的东西)

我不确定这是否有帮助,但下面是类单词列表中的代码(我没有写这个,它是给我的):


假设您的单词由
word
类描述,并且您有
List
(即
java.util.List
)个单词

您必须创建列表模型,该模型将作为
JList
的模型

比如:

WordList words = new WordList();

// (other GUI code in between)

JScrollPane scrollPane = new JScrollPane(DONT_KNOW);
    // you have list of words - wordList
    List<Word> wordList= new ArrayList<Word>();

    // create list model for JList
    DefaultListModel<Word> model = new DefaultListModel<Word>();
    // add all words from wordList to model
    for(Word word : wordList){
        model.addElement(word);
    }

    // create JList with model - model
    JList<Word> list = new JList<Word>(model);

   // create scroll pane for scrolling JList
    JScrollPane scrollPane = new JScrollPane(list);
public class WordList extends JList<Object> {....}
//您有单词列表-单词列表
List wordList=new ArrayList();
//为JList创建列表模型
DefaultListModel=新的DefaultListModel();
//将单词列表中的所有单词添加到模型中
for(单词:单词列表){
模型补遗(word);
}
//使用模型创建JList-模型
JList列表=新的JList(型号);
//创建滚动列表的滚动窗格
JScrollPane scrollPane=新的JScrollPane(列表);
如果您的word是
String
的实例,则可以将所有
word
替换为
String


还有一个提示,您可能已经直接创建了用于存储单词列表的列表模型(
DefaultListModel
),而不是
java.util.list
。因此,您不必维护两个列表(
DefaultListModel
java.util.List
)。

假设您的单词由
word
类描述,并且您有
List
(即
java.util.List
)单词

您必须创建列表模型,该模型将作为
JList
的模型

比如:

WordList words = new WordList();

// (other GUI code in between)

JScrollPane scrollPane = new JScrollPane(DONT_KNOW);
    // you have list of words - wordList
    List<Word> wordList= new ArrayList<Word>();

    // create list model for JList
    DefaultListModel<Word> model = new DefaultListModel<Word>();
    // add all words from wordList to model
    for(Word word : wordList){
        model.addElement(word);
    }

    // create JList with model - model
    JList<Word> list = new JList<Word>(model);

   // create scroll pane for scrolling JList
    JScrollPane scrollPane = new JScrollPane(list);
public class WordList extends JList<Object> {....}
//您有单词列表-单词列表
List wordList=new ArrayList();
//为JList创建列表模型
DefaultListModel=新的DefaultListModel();
//将单词列表中的所有单词添加到模型中
for(单词:单词列表){
模型补遗(word);
}
//使用模型创建JList-模型
JList列表=新的JList(型号);
//创建滚动列表的滚动窗格
JScrollPane scrollPane=新的JScrollPane(列表);
如果您的word是
String
的实例,则可以将所有
word
替换为
String


还有一个提示,您可能已经直接创建了用于存储单词列表的列表模型(
DefaultListModel
),而不是
java.util.list
。因此,您不必维护两个列表(
DefaultListModel
java.util.List
)。

我认为您的自定义类
WordList
应该继承
JList
,以使其工作(a.e作为
JScrollPane
的参数)

比如:

WordList words = new WordList();

// (other GUI code in between)

JScrollPane scrollPane = new JScrollPane(DONT_KNOW);
    // you have list of words - wordList
    List<Word> wordList= new ArrayList<Word>();

    // create list model for JList
    DefaultListModel<Word> model = new DefaultListModel<Word>();
    // add all words from wordList to model
    for(Word word : wordList){
        model.addElement(word);
    }

    // create JList with model - model
    JList<Word> list = new JList<Word>(model);

   // create scroll pane for scrolling JList
    JScrollPane scrollPane = new JScrollPane(list);
public class WordList extends JList<Object> {....}

我认为您的自定义类
WordList
应该继承
JList
,以使其工作(a.e作为
JScrollPane
的参数)

比如:

WordList words = new WordList();

// (other GUI code in between)

JScrollPane scrollPane = new JScrollPane(DONT_KNOW);
    // you have list of words - wordList
    List<Word> wordList= new ArrayList<Word>();

    // create list model for JList
    DefaultListModel<Word> model = new DefaultListModel<Word>();
    // add all words from wordList to model
    for(Word word : wordList){
        model.addElement(word);
    }

    // create JList with model - model
    JList<Word> list = new JList<Word>(model);

   // create scroll pane for scrolling JList
    JScrollPane scrollPane = new JScrollPane(list);
public class WordList extends JList<Object> {....}

在JScrollPane中使用JList

下面是一个例子

    JList list;
    DefaultListModel listModel;
    listModel = new DefaultListModel();
    listModel.addElement("word1");
    listModel.addElement("word2");
    listModel.addElement("word3");

    //Create the list and put it in a scroll pane.
    list = new JList(listModel);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setSelectedIndex(0);
    list.addListSelectionListener(this);
    list.setVisibleRowCount(5);
    JScrollPane listScrollPane = new JScrollPane(list);
上面的例子来自


在JList上签出。

在JScrollPane中使用JList

下面是一个例子

    JList list;
    DefaultListModel listModel;
    listModel = new DefaultListModel();
    listModel.addElement("word1");
    listModel.addElement("word2");
    listModel.addElement("word3");

    //Create the list and put it in a scroll pane.
    list = new JList(listModel);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setSelectedIndex(0);
    list.addListSelectionListener(this);
    list.setVisibleRowCount(5);
    JScrollPane listScrollPane = new JScrollPane(list);
上面的例子来自

在JList上结账