gpt4 book ai didi

Java无法在List中找到符号

转载 作者:行者123 更新时间:2023-12-02 11:22:16 25 4
gpt4 key购买 nike

好的,所以我从 JList 切换到 List,因为

1.) 它不与我绘制的图像重叠2.) 它可以禁用焦点但跟踪所选内容

无论如何,这是我尝试编译时遇到的错误:

C:\Users\Dan\Documents\DanJavaGen\inventory.java:30: cannot find symbol
symbol : constructor List(java.lang.Object[])
location: class java.awt.List
list = new List(arr.toArray());
^
C:\Users\Dan\Documents\DanJavaGen\inventory.java:50: cannot find symbol
symbol : method getSelectedValue()
location: class java.awt.List
Object index = list.getSelectedValue();
^

代码:

import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.io.*;
import java.util.*;
import java.awt.List;

public class inventory extends JApplet implements MouseListener {

public static String newline;
public static List 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 List(arr.toArray());

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) {

}
}

最佳答案

这里有两个问题:

java.awt.List 没有采用 Object[] 的构造函数:

list = new List();
for (String item : arr) {
list.add(item);
}

java.awt.ListgetSelectedItem()而不是getSelectedValue():

您可以将 ArrayList 与 List 一起使用,如下所示:

public void mouseReleased(MouseEvent e) {
Object index = list.getSelectedItem();
System.out.println("You have selected: " + index);
}

关于Java无法在List中找到符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3269599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com