简体   繁体   English

将ArrayList添加到Listbox Java中

[英]Add ArrayList into Listbox Java

So I'm trying to add an ArrayList to a Jlist, but I'm getting errors. 因此,我试图将ArrayList添加到Jlist,但出现错误。 Here is the code 这是代码

private void BReActionPerformed(java.awt.event.ActionEvent evt) {                                    
            NameList NL = new NameList();
            NL.name.add("a");
            NL.name.add("b");
            NL.name.add("c");
            NL.name.add("d");
            NL.name.add("e");
            for(int i=0; i < NL.name.size(); i++)
            {
                LOut.add(NL.name.get(i));
            }
}
LOut.add(NL.name.get(i));
  1. First of all learn an use Java naming standards. 首先学习使用Java命名标准。 Variable names do NOT start with an upper case character. 变量名不能以大写字母开头。

  2. You don't "add" items directly to the JList. 您不会直接将项目“添加”到JList。 You add items to the ListModel . 您将项目添加到ListModel Read the section from the Swing tutorial on How to Use Lists . 阅读Swing教程中有关如何使用列表的部分 The ListDemo code is a working example that shows how to add items to the DefaultListModel . ListDemo代码是一个有效的示例,显示了如何将项目添加到DefaultListModel The code also shows how to use proper variable names. 该代码还显示了如何使用适当的变量名。

Just remove the name field and try like below, 只需删除名称字段,然后尝试如下所示,

for(int i=0; i < NL.name.size(); i++)
{
   LOut.add(NL.get(i));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM