Java无法在List中找到符号

发布于 2024-09-10 09:59:42 字数 1871 浏览 3 评论 0原文

好的,所以我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

几味少女 2024-09-17 09:59:42

这里有两个问题:

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);
}

There are two issues at play here:

java.awt.List does not have a constructor that takes Object[]:

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

java.awt.List has getSelectedItem() not getSelectedValue():

You could your ArrayList with List as follows:

public void mouseReleased(MouseEvent e) {
    Object index = list.getSelectedItem();
    System.out.println("You have selected: " + index);
}
手心的温暖 2024-09-17 09:59:42
  • 您正在导入 java.util.Listjava.awt.List现在编译器对您想要哪一个感到困惑。有可能发生混淆。

  • java.awt.List 与 javax.swing.JList 不同,没有采用数组作为参数的构造函数。

  • 此外,您仍然有一个空的 paint() 方法。这是非常错误的。

  • You're importing both java.util.List and java.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文