JComboBox是一种原始类型。对泛型类型 JComboBox<E> 的引用应参数化

2022-09-03 04:04:55
String[] boxOptions = {"1","2","4","8","16","20","40","100","400"};
JComboBox box = new JComboBox(boxOptions);

我之前在我的程序中有这些确切的代码行,并且没有收到此错误。我做了一些搜索,我发现的结果有点超出我的脑海。有什么想法吗?

错误是:

JComboBox is a raw type. References to generic type JComboBox<E> should be parameterized

答案 1

您可以使用:

JComboBox<String> box = new JComboBox<>(boxOptions);

发生这种情况是因为现在是一个泛型类。JComboBox


答案 2

从Java 7开始,泛型被引入到JComboBox组件中。也许你以前使用Java6。您应该添加到那里的第二行。JComboBox<String>


推荐