警告:[rawtypes] 找到原始类型:DefaultListModel

发布于 2024-12-11 08:09:50 字数 767 浏览 3 评论 0原文

我从编译器收到这个警告,这根本没有意义(至少对我来说)。它基本上希望我将一个类型分配给 DefaultListModel,它本身就是一个对象类型!我在我的代码中收到了大量此类警告!

C:\Documents and Settings\...\filename.java:345:warning: [rawtypes] found raw type: DefaultListModel
DefaultListModel lm = (DefaultListModel) jList_DataSetList.getModel();
missing type arguments for generic class DefaultListModel<E>
where E is a type-variable:
E extends Object declared in class DefaultListModel

这是另外一个我不知道从哪里来的!

C:\Documents and Settings\...\filename.java:897: warning: [rawtypes] found raw type: JList
private javax.swing.JList jList_DataSetList;
missing type arguments for generic class JList<E>
where E is a type-variable:
E extends Object declared in class JList

提前致谢

I'm getting this warning from the compiler which does not make sense at all (at least to me). It basically wants me to assign a Type to DefaultListModel which is a object type by itself! I'm getting heaps of this warnings all through my code!

C:\Documents and Settings\...\filename.java:345:warning: [rawtypes] found raw type: DefaultListModel
DefaultListModel lm = (DefaultListModel) jList_DataSetList.getModel();
missing type arguments for generic class DefaultListModel<E>
where E is a type-variable:
E extends Object declared in class DefaultListModel

This is another one which I have no idea where is comes from!

C:\Documents and Settings\...\filename.java:897: warning: [rawtypes] found raw type: JList
private javax.swing.JList jList_DataSetList;
missing type arguments for generic class JList<E>
where E is a type-variable:
E extends Object declared in class JList

Thanks in advance

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

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

发布评论

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

评论(3

我做我的改变 2024-12-18 08:09:50

从 Java 7 开始,DefaultListModel 是一个泛型类型,如 ListSet 等。它需要一个类型:DefaultListModel 而不是原始的 DefaultListModel

这允许以更类型安全的方式工作,因为您将无法将 String 插入到应该包含 Integer 实例的列表模型中。从模型中获取元素时,您不必强制转换为 Integer。

对于 JList 也是如此,它现在也是字符串的 JList 或整数的 JList,而不是原始的 JList

阅读关于泛型的教程,并查看 < a href="http://download.oracle.com/javase/7/docs/api/javax/swing/DefaultListModel.html" rel="noreferrer">DefaultListModel 的 javadoc。

Since Java 7, DefaultListModel is a generic type, like List, Set, etc. It expects a type : DefaultListModel<SomeClass> instead of the raw DefaultListModel.

This allows to work in a more type-safe way, because you won't be able to insert a String into a list model which is supposed to contain Integer instances. And you won't have to cast to Integer when getting an element from the model.

The same is true for JList which is also now a JList of Strings or a JList of Integers, instead of being a raw JList.

Read the tutorial about generics, and have a look at the javadoc of DefaultListModel.

剩余の解释 2024-12-18 08:09:50

尝试 @SuppressWarnings("rawtypes")

当使用反射或使用应该隐藏泛型对象的确切类型的接口方法时,这是一个特别糟糕的默认值。当我想起@Suppress 时,我开始将类引用更改为类。

Try @SuppressWarnings("rawtypes")

This is a particularly awful default when working with reflection or when working with interfaced methods where the exact type of the generic object is supposed to be hidden. I started changing my Class references to Class when I remembered @Suppress.

你爱我像她 2024-12-18 08:09:50

如果有人在 NetBeans IDE (7.0.1) 生成的代码(针对 JList 等)中查找这些警告,而您不知道如何纠正它,请按照以下步骤操作:

  1. 转到 GUI 编辑器(matisse 生成器)。
  2. 选择(单击)组件(JList 等)
  3. 转至“属性”窗口,转至“代码”选项卡
  4. 写入进入类型参数属性。当然,“某物”可以是字符串,或者你的对象......

And if someone is looking for these warnings in the NetBeans IDE (7.0.1) generated code (for JList etc.) and you don't know, how to correct it, follow these steps:

  1. Go to GUI editor (matisse builder).
  2. Select (click on) the component (JList, etc.)
  3. Go to Properties window, to Code tab
  4. Write the <Something> into Type Parametres property. Of course, the "Something" would be String, or your object...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文