注意:anpr.java使用未检查或不安全的操作注意:重新编译-xlint:unchecked for jcombobox中的详细信息警告

bn31dyow  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(289)

在我使用jcombobox试图隐藏它之后,警告出现了 @SuppressWarnings("deprecation") 但在我重新编译之后就不起作用了-xlint:unchecked it 显示 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);

 }

}

btxsgosb

btxsgosb1#

编译器警告您,您定义jcombobox的方式可以将任何对象添加到组合框中。
generics 如果添加到java中,则更安全的做法是指定要在组合框中显示的数据类型,然后编译器可以检查以确保只将特定的数据类型添加到组合框中。
因此,要消除您使用的警告信息:

JComboBox<String> gen;

定义组合框时。
创建组合框示例时,可以使用:

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

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

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

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

相关问题