来自编译器的未经检查的强制转换警告

我正在尝试创建一个游戏。它几乎已经准备好了,但是当我尝试对其进行编译时,我得到了未经检查的强制转换警告。 这是我的代码的开头:

public class GUI extends JFrame implements ListSelectionListener {

    private Tiles tiles;
    private ConfigurationStore st;

    public GUI(ConfigurationStore cs) {
        super("My Game");
        st = cs;
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(1000,700);

        add(ConfigurationsPanel(), BorderLayout.WEST);
    }

这是我的ConfigurationsPanel():

    private JPanel ConfigurationsPanel() {
        JPanel conf = new JPanel(new BorderLayout());
        addBorder(conf, "Configurations");
        ArrayList<Configuration> configurations = new ArrayList<>();
        configurations = store.getConfigurations();
        Configuration[] configurationsArray = new Configuration[configurations.size()];
        for(int i = 0; i < configurations.size(); i++) {
            configurationsArray[i] = configurations.get(i);
        }
        JList<Configuration> list = new JList<Configuration>(configurationsArray);
        list.addListSelectionListener(this);
        JScrollPane scrollPane = new JScrollPane(list);
        conf.add(scrollPane);
        return conf;
    }

这是我的valueChanged方法:

    public void valueChanged(ListSelectionEvent e) {
        JList<Configuration> list = (JList<Configuration>) e.getSource();
        Configuration configuration = list.getSelectedValue();
        tiles = new Tiles(c);
    }

Compiler gives warning in the valueChanged method where I wrote JList<Configuration> list = (JList<Configuration>) e.getSource(); by saying: required: JList<Configuration> found: Object. Can you please tell me what part of my code is not right and how can I remove that warning. Thanks in advance.

评论
  • ad_est
    ad_est 回复

    阅读并理解警告。

    承认JVM不能保护您免受不良转换的影响,并承担知道自己在做什么的责任。

    然后取消警告:

    public void valueChanged(ListSelectionEvent e) {
        @SuppressWarnings("unchecked")
        JList<Configuration> list = (JList<Configuration>) e.getSource();
        ...