Java ArrayLists转换为JList
好,所以我只占了库存的一小部分。我记不清了。我试图将字符串项添加到ArrayList,然后将其添加到JList。但是,在编译时出现此错误:
C:\Users\Dan\Documents\DanJavaGen\inventory.java:30: cannot find symbolsymbol : constructor JList(java.util.ArrayList<java.lang.String>)
location: class javax.swing.JList
list = new JList(arr);
我可能正在犯一些菜鸟错误…:/
码:
import java.applet.Applet;import java.awt.*;
import javax.swing.*;
import javax.swing.JList;
import java.awt.event.*;
import java.util.ArrayList;
import java.io.*;
import java.util.*;
public class inventory extends JApplet implements MouseListener {
public static String newline;
public static JList list;
int gold = 123;
public void init() {
ArrayList<String> arr = new ArrayList<String>();
arr.add("Hatchet");
arr.add("Sword");
arr.add("Shield");
arr.add(gold + " Gold");
System.out.println("You have " + arr.size() + " items in your inventory.");
showInventory(arr);
list = new JList(arr);
add(list);
list.addMouseListener(this);
list.setVisible(true);
}
public static void showInventory (ArrayList<String> theList) {
for (int i = 0; i < theList.size(); i++) {
System.out.println(theList.get(i));
}
}
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) {
Object index = list.getSelectedValue();
System.out.println("You have selected: " + index);
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void paint(Graphics g) {
}
}
回答:
,您可以在使用 解 后调用该构造函数: *
list = new JList(arr.toArray())
以上是 Java ArrayLists转换为JList 的全部内容, 来源链接: utcz.com/qa/400258.html