简体   繁体   English

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

[英].java uses unchecked and unsafe operation

here is my java code: 这是我的Java代码:

import javax.swing.*;

public class Employee1 extends JFrame {

    JPanel panel;
    JLabel l1;
    JList list;

    public Employee1() {
        super("Employee Details");
        panel = new JPanel();
        l1 = new JLabel("City : ");
        String cities[] = {"Mumbai", "Delhi", "Madras"};
        list = new JList(cities);
        panel.add(l1);
        panel.add(list);
        getContentPane().add(panel);
        setSize(400, 400);
        setVisible(true);
    }

    public static void main(String args[]) {
        Employee1 obj = new Employee1();
    }
}

this code gives me warning .java uses unchecked and unsafe operation. 这段代码给我警告。java使用未经检查和不安全的操作。 i have exams so please help me to go through it . 我有考试,所以请帮助我进行考试

You should make use of a type parameter for your JList because this is a generics error and JList supports genercis. 您应该为JList使用类型参数,因为这是一个泛型错误,并且JList支持泛型。

Change: 更改:

JList list to JList<String> list JList listJList<String> list
and list = new JList(cities) to list = new JList<>(cities) list = new JList(cities)list = new JList<>(cities)

public class Employee1 extends JFrame {
    private final JPanel panel;
    private final JLabel l1;
    private final JList<String> list;    // <--- first change

    public Employee1() {
        super("Employee Details");

        final String[] cities = {"Mumbai", "Delhi", "Madras"};

        panel = new JPanel();
        l1    = new JLabel("City : ");
        list  = new JList<>(cities);     // <--- second change

        panel.add(l1);
        panel.add(list);
        getContentPane().add(panel);

        setSize(400, 400);
        setVisible(true);
    }    
}

See Lesson: Generics for information and examples on this topic 有关此主题的信息和示例,请参见课程:泛型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM