警告:[rawtypes] 找到原始类型:DefaultListModel
我从编译器收到这个警告,这根本没有意义(至少对我来说)。它基本上希望我将一个类型分配给 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 Java 7 开始,
DefaultListModel
是一个泛型类型,如List
、Set
等。它需要一个类型: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, likeList
,Set
, etc. It expects a type :DefaultListModel<SomeClass>
instead of the rawDefaultListModel
.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 aJList
of Strings or aJList
of Integers, instead of being a rawJList
.Read the tutorial about generics, and have a look at the javadoc of DefaultListModel.
尝试 @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.
如果有人在 NetBeans IDE (7.0.1) 生成的代码(针对 JList 等)中查找这些警告,而您不知道如何纠正它,请按照以下步骤操作:
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: