Flower.java使用未经检查或不安全的操作

时间:2018-04-14 11:36:27

标签: java

运行以下命令后,编译器会发出2个警告:

Flower.java:33: warning: [unchecked] unchecked cast
        updateComboBox((JComboBox<String>) formComponents.get("myComboBox"));
                                                             ^
  required: JComboBox<String>
  found:    Component

第一次警告:

 Flower.java:36: warning: [unchecked] unchecked cast
                JComboBox<String> cmbProfCourseNo = (JComboBox<String>) formComponents.get("myComboBox");
                                                                                          ^
  required: JComboBox<String>
  found:    Component

第二次警告:

DefaultComboBoxModel<String> model = (DefaultComboBoxModel<String>) comboBox.getModel();

我尝试将其设为通用,如下所示,但没有变化。

import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.util.Map;
import java.util.HashMap;


class Flower 
{   
    //declare the frame
    private JFrame frame;

    //declare HashMap
    private Map<String, Component> formComponents= new HashMap<String, Component>();

    //main method of the class 
    public static void main(String[] args) {
        new Flower().init();
    }

    public void init() {
        frame = new JFrame("Flower");
        frame.setSize(650, 725);
        frame.setLayout(null);

        formComponents.put("myComboBox", new JComboBox<String>());
        formComponents.get("myComboBox").setBounds(125, 165, 200, 25);
        frame.add(formComponents.get("myComboBox"));
        frame.setVisible(true);

        //update comboBox
        updateComboBox((JComboBox<String>) formComponents.get("myComboBox"));

        //get comboBox
        JComboBox<String> cmbProfCourseNo = (JComboBox<String>) formComponents.get("myComboBox");
    }


    public void updateComboBox(JComboBox<String> comboBox) {        
        DefaultComboBoxModel<String> model = (DefaultComboBoxModel<String>) comboBox.getModel();        
        model.addElement("Car");
        //set the model
        comboBox.setModel(model);
    }
}

Flower.java完整源代码是:

 @RequestMapping(method = RequestMethod.GET , value = "/availabletimes")
public ResponseEntity<ResponseUtil> getAvailableTimes(@RequestParam String date){

    try {
        return new ResponseEntity<ResponseUtil> (new ResponseUtil(200, Constants.SUCCESS_STATUS, "Cancelled", service.getAvailableTimes(date)),HttpStatus.OK);
    }catch (BackendException e) {
        return new ResponseEntity<ResponseUtil> (new ResponseUtil(403, Constants.FAILED_STATUS, e.getMessage(), null),HttpStatus.FORBIDDEN);
    }

}

我的HashMap必须存储JLabel,JTextField和其他JComponents,因此我将其类型声明为Component(因为Component是Swing层次结构中的上层类)。

1 个答案:

答案 0 :(得分:2)

public void updateComboBox(JComboBox<String> comboBox) {
    //get the combo box model
    DefaultComboBoxModel<String> model = (DefaultComboBoxModel<String>) comboBox.getModel();

    model.addElement("Car");
    //set the model
    comboBox.setModel(model);
}

但那些刚刚交战,编译器可以处理它们......