Java 在铸造JComboBox之前,如何检查它是哪种类型?
我正在尝试创建一种方法来清除JFrame中的所有字段。但是我遇到了来自Eclipse的警告Java 在铸造JComboBox之前,如何检查它是哪种类型?,java,swing,casting,warnings,type-safety,Java,Swing,Casting,Warnings,Type Safety,我正在尝试创建一种方法来清除JFrame中的所有字段。但是我遇到了来自Eclipse的警告 private void clearAll(Container container) { for (Component component : container.getComponents()) { if (component instanceof JTextField) { JTextField field = (JTextFi
private void clearAll(Container container) {
for (Component component : container.getComponents()) {
if (component instanceof JTextField) {
JTextField field = (JTextField) component;
field.setText("");
}
if (component instanceof JComboBox) {
JComboBox<String> box = (JComboBox<String>) component;
box.setSelectedIndex(-1);
}
if (component instanceof Checkbox) {
Checkbox box = (Checkbox) component;
box.setState(false);
}
if (component instanceof Container) {
clearTextFields((Container) component);
}
}
}
我收到一条不同的警告信息:
JComboBox是一种原始类型。对泛型类型JComboBox的引用应该参数化
我是swing新手,所以我不知道所有的方法/特性。如果我重置一切的方法可以更容易/更好地完成,请通知我。我获得了从网站上的另一篇文章中清除所有字段的原始方法。如何:
if (component instanceof JComboBox) {
JComboBox<?> box = (JComboBox<?>) component;
box.setSelectedIndex(-1);
}
if(JComboBox的组件实例){
JComboBox=(JComboBox)组件;
框。设置所选索引(-1);
}
JComboBox是一个泛型类,当您使用concret类型进行转换时,第二个警告将消失。谢谢,我不知道您可以这样做。
if (component instanceof JComboBox) {
JComboBox<?> box = (JComboBox<?>) component;
box.setSelectedIndex(-1);
}