Java在列表中找不到符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3269599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java cannot find symbol in List
提问by test
OK so I switched from JList to List because
好的,所以我从 JList 切换到 List 因为
1.) It doesn't overlap my drawn images 2.) It can have focus disabled yet track what's selected
1.) 它不会与我绘制的图像重叠 2.) 它可以禁用焦点但跟踪所选内容
Anyway, here's the error I get when I try to compile:
无论如何,这是我尝试编译时遇到的错误:
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();
^
The code:
编码:
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) {
}
}
采纳答案by Alain O'Dea
There are two issues at play here:
这里有两个问题在起作用:
java.awt.Listdoes not have a constructor that takes Object[]:
java.awt.List没有接受Object[]的构造函数:
list = new List();
for (String item : arr) {
list.add(item);
}
java.awt.Listhas getSelectedItem()not getSelectedValue():
java.awt.List有getSelectedItem()而不是getSelectedValue():
You could your ArrayList with List as follows:
您可以将 ArrayList 与 List 如下:
public void mouseReleased(MouseEvent e) {
Object index = list.getSelectedItem();
System.out.println("You have selected: " + index);
}
回答by Carl Smotricz
You're importing both
java.util.List
andjava.awt.List
.Now the compiler is confused about which one you want.There's a possibility of confusion.java.awt.List, unlike javax.swing.JList, doesn't have a constructor that takes an array as an argument.
Also, you still have an empty
paint()
method. That's very wrong.
您正在导入
java.util.List
和java.awt.List
。现在编译器对你想要哪个感到困惑。有混淆的可能。java.awt.List 与 javax.swing.JList 不同,它没有将数组作为参数的构造函数。
此外,您仍然有一个空
paint()
方法。这是非常错误的。