我无法摆脱 java7 中的一个新的类型安全警告。
我定义了以下 JCombobox 对象
private JComboBox<Integer> combobox_current_year;
以及构造函数
combobox_current_year = new JComboBox(options.getList_years().toArray());
Java 7 现在给我以下警告:
Type safety: The expression of type JComboBox needs unchecked conversion to conform to JComboBox
将代码更改为
combobox_current_year = new JComboBox<Integer>((Integer[]) options.getList_years().toArray());
我收到以下异常:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer; at jamm.gui.FinanzmanagerGui.mainWindow(FinanzmanagerGui.java:123) at jamm.StartJamm$1.run(StartJamm.java:43) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705) at java.awt.EventQueue.access$000(EventQueue.java:101) at java.awt.EventQueue$3.run(EventQueue.java:666) at java.awt.EventQueue$3.run(EventQueue.java:664) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:675) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105) at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
最佳答案
尝试做
combobox_current_year = new JComboBox<Integer>(options.getList_years().toArray(new Integer[0]));
集合框架的toArray总是返回一个Object[],如果你想要一个特定的数组类型,你需要提供一个
关于java - Java 7 上 JCombobox 的类型安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8077455/