【发布时间】:2023-03-17 09:16:01
【问题描述】:
我一直在尝试使用构造函数使用数组列表填充 Eclipse GUI Java JComboBox,但没有任何运气。这是我迄今为止尝试过的。
import item.Item;
import javax.swing.JComboBox;
import java.util.ArrayList;
public class SelectionScreen{
private JFrame frame;
static ArrayList< Item> list;
private String items;
public static void main (String[] args){
initialize();
}
public void initialize(){
list = new ArrayList< Item >();
list.add(new Item("Strawberry,200,.25,.75);
list.add(new Item("Banana,200,.25,1.00);
list.add(new Item("Oranges,200,.25,2.00);
JcomboBox comboBox = newJcomboBox();
ComboBox.setBounds(63,29,86,22)
frame.getContentPane().add(comboBox);
// here is where I tried to fill the combobox
//comboBox.setModel(new DefaultComboBoxModel(Item.getName()))); //Wrong
//comboBox.setModel(Item.getName); //Wrong
//the following only loads the last item in the list which is Oranges
for(Item i: list{
comboBox.setModel(new DefaultComboBoxModel(New String[] {
i.getName()}));
}
// tried making a different list to collect my fruits.
for(Item i: list){
list2[ i.getName()];
Item.length;
} //which was a complete fail.
我完全迷路了,对 Java 的经验也不是很丰富。我可以很好地使用
comboBox.setModel(new DefaultComboBoxModel(new String[]{ "Strawberry","Banana","Oranges"}));
但是当我从文本文件中导入它们时,我不会知道列表中有哪些水果。
任何帮助将不胜感激。
【问题讨论】:
标签: java swing arraylist jcombobox