java 如何将数组字符串添加到 Netbeans 中的列表框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2997116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:52:34  来源:igfitidea点击:

How to add a Array String to a list box in Netbeans

javanetbeans

提问by Nubkadiya

I want to add a String Array to a list box in Netbeans.

我想将一个字符串数组添加到 Netbeans 的列表框中。

回答by Uros K

String[] arr = {"one", "two", "three"};
listbox.setListData(arr);    //listbox is your JList object

See: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JList.html

见:http: //java.sun.com/j2se/1.4.2/docs/api/javax/swing/JList.html

回答by ThePCWizard

I prefer to do it like this:

我更喜欢这样做:

DefaultListModel list = new DefaultListModel();
for (int i = 0; i < data.length; i++) {
    list.addElement(data[i]);
}      
jList1.setModel(list);

回答by npinti

If you want to add the items through the GUI builder tool, do as follows:

如果您想通过 GUI builder 工具添加项目,请执行以下操作:

Drag and Drop the JList Component onto your parent container

将 JList 组件拖放到您的父容器上

Select the List component you have just dragged by clicking on it and on the right hand side, chose Properties.

通过单击选择您刚刚拖动的 List 组件,然后在右侧选择 Properties。

From the list of items at the top (like Background, Border, Font, etc) click on Model (by default it has the following items in it: Item 1, Item 2, etc.). Click on the button that has three ellipses (it is on the same row) and a window should open. It should have the following items:

从顶部的项目列表(如背景、边框、字体等)单击模型(默认情况下,其中包含以下项目:项目 1、项目 2 等)。单击具有三个椭圆的按钮(位于同一行),应打开一个窗口。它应该有以下项目:

  • Item 1
  • Item 2
  • Item 3, etc.
  • 第 1 项
  • 第 2 项
  • 第 3 项等。

Delete those and put in the items you want to add and press ok.

删除这些并放入要添加的项目,然后按确定。

That is how it is done through the Netbeans GUI builder. If you want to do it programmatically, do as genessis suggets.

这就是通过 Netbeans GUI 构建器完成的。如果您想以编程方式执行此操作,请按照创世纪的建议进行操作。