Java ArrayList 转换为 JList
好的,我正在做库存的一小部分。我把大部分都记下来了。我正在尝试将字符串项添加到 ArrayList,然后将其添加到 JList。但是,我在编译时遇到此错误:
C:\Users\Dan\Documents\DanJavaGen\inventory.java:30: cannot find symbol
symbol : 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) {
}
}
OK so I'm doing a small part of my inventory. I got MOST of it down. I'm trying to add string items to an ArrayList then add that to a JList. However, I'm getting this error when I compile:
C:\Users\Dan\Documents\DanJavaGen\inventory.java:30: cannot find symbol
symbol : constructor JList(java.util.ArrayList<java.lang.String>)
location: class javax.swing.JList
list = new JList(arr);
It's probably some rookie mistake I am making ... :/
Code:
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) {
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
JList 提供了一个构造函数 JList(E[]),您可以在使用 toArray(T[ 解压 ArrayList 后调用该构造函数]):
JList provides a constructor JList(E[]) which you can call after unpacking your ArrayList<String> using toArray(T[]):
试试这个:
Try this:
我的项目中的 toArray() 和 JList 也遇到了麻烦。做了一些研究并尝试了一些事情,发现了一些有效的方法。希望对大家有帮助:
I had trouble with toArray() and JList for my project as well. Did some research and tried a few things and found something that works. Hope it helps everyone:
我在使用
toArray()
方法时遇到了导致异常的问题,因此我构建了一个快速通用方法来进行转换。也许有人也会发现它很有用。我知道这是一篇旧帖子,但我敢打赌它仍然时不时地被浏览。方法如下:只需在传入数组之前创建数组,就像
然后从代码中调用它一样
I had trouble with the
toArray()
method causing exceptions, so I built a quick generic method to convert. Maybe someone will find it useful as well. I know this is an old post but I'll bet it's still viewed from time to time. Here's the method:Just create the array before you pass it in, like
Then just call it from your code
构造函数摘要
JList()
使用空的只读模型构造JList
。JList(ListModel dataModel)
构造一个JList
,显示指定的非空模型中的元素。JList(Object[] listData)
构造一个JList
,显示指定数组中的元素。JList(Vector listData)
构造一个JList
,显示指定 Vector 中的元素。Constructor Summary
JList()
Constructs aJList
with an empty, read-only, model.JList(ListModel dataModel)
Constructs aJList
that displays elements from the specified, non-null, model.JList(Object[] listData)
Constructs aJList
that displays the elements in the specified array.JList(Vector<?> listData)
Constructs aJList
that displays the elements in the specified Vector.我只是实现了 ListModel 接口:
I simply implemented the ListModel interface:
如果您使用拖放组件,则将列表命名为“itemList”,我认为 arr 是包含一些字符串数据的数组列表的名称:
我希望这对您有用。
if you are using drag and drop components then, name your list as "itemList" and I suppose that arr is the name of your arraylist which contains some string data then :
I hope this will work for you.
您可以将
Object
超类作为 JList 的类型传递。You can pass
Object
superclass as type of the JList.如果你有一个循环来获取 mysql 中的数据
你可以这样放
if you have a loop in for gaining data in mysql
you can put like this