在这种情况下,如何获取已添加到JList中的字符串项目的项目值?我的意思是V
String[] coinNames ={"Quarters","Dimes","Nickels","Pennies"};
JList coinList = new JList (coinNames);
coinList[0] == "Quarters" ???????
由于我显然无法像普通数组一样引用它,我该如何获取coinlist [0]的字符串值?
最佳答案
这是一个简单的示例,仅获取您的JList的索引并显示所有JList。
public static void main(String[] args) {
String[] coinNames ={"Quarters","Dimes","Nickels","Pennies"};
JList coinList = new JList (coinNames);
String myCoin = (String) coinList.getModel().getElementAt(0);
System.out.println(myCoin);
/* LIST AL YOUR COINNAMES */
System.out.println("\nCoinList\n");
for(int i =0; i < coinList.getModel().getSize(); i++){
System.out.println((String) coinList.getModel().getElementAt(i));
}
coinNames[0] = "My new Value"; // Edit your CoinNames at index 0
/* LIST AL YOUR NEW COINNAMES */
System.out.println("\nNew coinList edited\n");
for(int i =0; i < coinList.getModel().getSize(); i++){
System.out.println((String) coinList.getModel().getElementAt(i));
}
}