如何在这种情况下获取已添加到JList的String项的值?我的意思是V
String[] coinNames ={"Quarters","Dimes","Nickels","Pennies"};
JList coinList = new JList (coinNames);
coinList[0] == "Quarters" ???????
由于我显然不能像普通数组那样引用它,我怎样才能获得coinlist [0]的字符串值?
答案 0 :(得分:3)
这是一个简单的示例,只获取JList的索引并显示所有JList。
int
答案 1 :(得分:2)
coinList.getModel().getElementAt(0);
请阅读手册:http://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html#getModel--
编辑:或者只是看一下该页面的操作示例:http://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html
// Create a JList that displays strings from an array
String[] data = {"one", "two", "three", "four"};
JList<String> myList = new JList<String>(data);
// Create a JList that displays the superclasses of JList.class, by
// creating it with a Vector populated with this data
Vector<Class<?>> superClasses = new Vector<Class<?>>();
Class<JList> rootClass = javax.swing.JList.class;
for(Class<?> cls = rootClass; cls != null; cls = cls.getSuperclass()) {
superClasses.addElement(cls);
}
JList<Class<?>> myList = new JList<Class<?>>(superClasses);
// The automatically created model is stored in JList's "model"
// property, which you can retrieve
ListModel<Class<?>> model = myList.getModel();
for(int i = 0; i < model.getSize(); i++) {
System.out.println(model.getElementAt(i));
}