列表项 = new ArrayList() :它不起作用

发布于 2025-01-05 09:54:55 字数 1397 浏览 1 评论 0原文

都在标题里了 我不明白这次的问题有点不同,我对两个不同的程序使用了相同的对象(列表),但第二次不起作用,请参阅:

private void jMenuItem23ActionPerformed(java.awt.event.ActionEvent evt) {                                            
init_creer_client();
List  items  = new ArrayList();
items.add("mawren");
items.add("blabla");
items.add("Bonjour");

CL.show(cartes,"creer_client");       
}   

有关错误的屏幕截图: 在此处输入图像描述

通过 cons 这里,它的工作顺利:

 import java.awt.Dimension;
 import java.awt.HeadlessException;
 import java.util.ArrayList;
 import java.util.List;
 import javax.swing.JFrame;
 import javax.swing.JPanel;
 import javax.swing.JTextField;
 import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

 public class Test_swingx extends JFrame {

public Test_swingx(String title) throws HeadlessException {

this.setTitle(title);
JPanel pan=new JPanel();
JTextField jtf=new JTextField();
jtf.setColumns(20);
List items  = new ArrayList();
items.add("hello");
items.add("marwen");
items.add("allooo");
AutoCompleteDecorator.decorate(jtf, items,false);
pan.add(jtf);
this.setContentPane(pan);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(280, 150, 500, 200);

}


 public static void main(String[] args) {

Test_swingx tsx=new Test_swingx("helloo swingx");

}
}

任何人都可以向我解释一下吗?

Is all in the title,
I do not understand the problem this time is a bit different, I used the same Object(List) for two different programs and it does not work in the second time, see :

private void jMenuItem23ActionPerformed(java.awt.event.ActionEvent evt) {                                            
init_creer_client();
List  items  = new ArrayList();
items.add("mawren");
items.add("blabla");
items.add("Bonjour");

CL.show(cartes,"creer_client");       
}   

screenshot about the error :
enter image description here

by cons here its work smoothly :

 import java.awt.Dimension;
 import java.awt.HeadlessException;
 import java.util.ArrayList;
 import java.util.List;
 import javax.swing.JFrame;
 import javax.swing.JPanel;
 import javax.swing.JTextField;
 import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

 public class Test_swingx extends JFrame {

public Test_swingx(String title) throws HeadlessException {

this.setTitle(title);
JPanel pan=new JPanel();
JTextField jtf=new JTextField();
jtf.setColumns(20);
List items  = new ArrayList();
items.add("hello");
items.add("marwen");
items.add("allooo");
AutoCompleteDecorator.decorate(jtf, items,false);
pan.add(jtf);
this.setContentPane(pan);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(280, 150, 500, 200);

}


 public static void main(String[] args) {

Test_swingx tsx=new Test_swingx("helloo swingx");

}
}

can anyone explain to me ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

优雅的叶子 2025-01-12 09:54:55

您有一个 java.awt.List 导入应该是 java.util.List

You have a java.awt.List import should be java.util.List

平定天下 2025-01-12 09:54:55

这是因为左侧的 Listjava.awt.List 而不是 java.util.List

尝试将该行更改为:

java.util.List items = new ArrayList();

发生这种情况可能是因为您正在导入 java.awt.* java.util.List。如果您可以更改导入这些类的方式,则可以避免内联类型的命名空间。

It's because the List on the left-hand side is a java.awt.List instead of a java.util.List.

Try changing the line to:

java.util.List items = new ArrayList();

This is probably happening because you're importing java.awt.* and java.util.List. If you can change how you import these classes, you can avoid namespacing the type inline.

口干舌燥 2025-01-12 09:54:55

不,编译良好:

package cruft;

import java.util.ArrayList;
import java.util.List;

/**
 * ListExample description here
 * @author Michael
 * @link
 * @since 2/11/12 7:27 PM
 */
public class ListExample {

    public static void main(String[] args) {
        List items = new ArrayList();
        for (String arg : args) {
            items.add(arg);
        }
        System.out.println(items);
    }
}

运行良好:

"C:\Program Files\Java\jdk1.7.0_02\bin\java" -Didea.launcher.port=7536 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 111.255\bin" -Dfile.encoding=UTF-8 -classpath . com.intellij.rt.execution.application.AppMain cruft.ListExample foo bar baz bat
[foo, bar, baz, bat]

Process finished with exit code 0

Nope, compiles fine:

package cruft;

import java.util.ArrayList;
import java.util.List;

/**
 * ListExample description here
 * @author Michael
 * @link
 * @since 2/11/12 7:27 PM
 */
public class ListExample {

    public static void main(String[] args) {
        List items = new ArrayList();
        for (String arg : args) {
            items.add(arg);
        }
        System.out.println(items);
    }
}

Runs fine:

"C:\Program Files\Java\jdk1.7.0_02\bin\java" -Didea.launcher.port=7536 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 111.255\bin" -Dfile.encoding=UTF-8 -classpath . com.intellij.rt.execution.application.AppMain cruft.ListExample foo bar baz bat
[foo, bar, baz, bat]

Process finished with exit code 0
‖放下 2025-01-12 09:54:55

健全性检查:您是否同时导入了 import java.util.Listimport java.util.ArrayList

Sanity check: Have you imported both import java.util.List and import java.util.ArrayList?

酒与心事 2025-01-12 09:54:55

检查您的导入,因为 java.awt.Listjava.util.List 不同。

Check your imports, because java.awt.List is not the same as java.util.List.

够钟 2025-01-12 09:54:55

我认为混乱来自于不同包中的两个 List 类型,正如错误消息所述。您没有提供生成错误的所有代码,但我认为修复的合理开始是将突出显示的行更改为:

java.util.List items  = new ArrayList();

并确保您已导入 java.util.*

I think the confusion comes from having two List types in different packages, as the error message says. You don't give all the code that generates the error, but I think a reasonable start to a fix would be to change the highlighted line to:

java.util.List items  = new ArrayList();

and make sure you have imported java.util.*

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