Java无法在List中找到符号
好的,所以我从 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) {
}
}
OK so I switched from JList to List because
1.) It doesn't overlap my drawn images
2.) It can have focus disabled yet track what's selected
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) {
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有两个问题:
java.awt.List 没有采用 Object[] 的构造函数:
java.awt.List有 getSelectedItem() 而不是 getSelectedValue():
您可以将 ArrayList 与 List 一起使用,如下所示:
There are two issues at play here:
java.awt.List does not have a constructor that takes Object[]:
java.awt.List has getSelectedItem() not getSelectedValue():
You could your ArrayList with List as follows:
您正在导入
java.util.List
和java.awt.List
。现在编译器对您想要哪一个感到困惑。有可能发生混淆。java.awt.List 与 javax.swing.JList 不同,没有采用数组作为参数的构造函数。
此外,您仍然有一个空的
paint()
方法。这是非常错误的。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.