注意:Anpr.java 使用未经检查或不安全的操作。注意:在 JComboBox 中使用 -Xlint:unchecked 重新编译以获取详细信息警告

Note: Anpr.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details warning in JComboBox

在我使用 JComboBox 后出现警告我试图用 @SuppressWarnings("deprecation") 隐藏它但是在我用 -Xlint 重新编译它后它没有工作:未选中它显示 Anpr.java:61: warning: [unchecked] unchecked call to JComboBox(E[]) as a member of the raw type JComboBox gen = new JComboBox(new String[] {"Male","Female"}); where E is a type-variable:E extends Object declared in class JComboBox

import javax.swing.*;
@SuppressWarnings("deprecation")
public class Anpr extends JFrame{
            ImageIcon icon; 
            ImageIcon img3;
            JLabel imglab3,label3,label4,label5,label6,label7,label8,label9,label10;
            Dimension dimension;
            JTextField pid,name,cno,age,bgrp,addr,anydis;
            JComboBox gen; 
     Anpr(){    
                dimension = Toolkit.getDefaultToolkit().getScreenSize();      
                int x=(int)((dimension.getWidth() - 650)/2);
                int y=(int)((dimension.getHeight() - 650)/2);
                setLocation(x, y);
                setSize(600,600);
                setLayout(null);
                icon = new ImageIcon("image/medical-record.png");
                setIconImage(icon.getImage());
                setTitle("Add New Patient Record");
                setResizable(false);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                label3 = new JLabel("Patient ID");
                label3.setBounds(150,50,80,30);
                label4 = new JLabel("Name");
                label4.setBounds(150,90,80,30);
                label5 = new JLabel("Contact No");
                label5.setBounds(150,130,80,30);
                label6 = new JLabel("Age");
                label6.setBounds(150,170,80,30);
                label7 = new JLabel("Gender");
                label7.setBounds(150,210,80,30);
                label8 = new JLabel("Blood Group");
                label8.setBounds(150,250,80,30);
                label9 = new JLabel("Address");
                label9.setBounds(150,290,80,30);
                label10 = new JLabel("Any Major Disease Suffered Earlier");
                label10.setBounds(150,330,200,30);
                add(label3);
                add(label4);
                add(label5);
                add(label6);
                add(label7);
                add(label8);
                add(label9);
                add(label10);
                JButton btnsa=new JButton("Save");
                JButton btnco=new JButton("Close");
                btnsa.setBounds(150,450,80,30);
                btnco.setBounds(350,450,80,30);
                add(btnsa);
                add(btnco);
                pid = new JTextField();
                pid.setBounds(270,50,180,30);
                name = new JTextField();
                name.setBounds(270,90,180,30);
                cno = new JTextField();
                cno.setBounds(270,130,180,30);
                age = new JTextField();
                age.setBounds(270,170,180,30);
                gen = new JComboBox(new String[] {"Male","Female"});
                gen.setBounds(270,210,180,30);
                bgrp = new JTextField();
                bgrp.setBounds(270,250,180,30);
                addr = new JTextField();
                addr.setBounds(270,290,180,30);
                anydis = new JTextField();
                anydis.setBounds(150,370,300,30);
                 add(pid);
                 add(name);
                 add(cno);
                 add(age);
                 add(gen);
                 add(bgrp);
                 add(addr);
                 add(anydis);
                /*Background image 
                img3 = new ImageIcon("image/m_back2.jpg");
                imglab3 = new JLabel(img3);
                imglab3.setSize(600,600);
                add(imglab3);*/
            }     
    public static void main(String[] args){
         Anpr apr=new Anpr();
        apr.setVisible(true); 
        
     }
  }

编译器警告您定义 JComboBox 的方式可以将任何对象添加到组合框。

因为 generics 被添加到 Java 中,所以在组合框中指定要显示的数据类型更安全,然后编译器可以检查以确保您只添加特定的数据组合框的数据类型。

因此,要删除您使用的警告消息:

JComboBox<String> gen;

定义组合框时。

当您创建组合框的实例时,您可以使用:

gen = new JComboBox<String>(new String[] {"Male","Female"});

或者,更简单的是只使用“<>”,字符串将默认来自变量声明:

gen = new JComboBox<>(new String[] {"Male","Female"});

阅读“泛型”了解更多信息。