java编译中的未检查或不安全操作错误?

时间:2022-11-09 15:24:45

I am completing a lab assignment for school and get this error when I compile. The program runs fine, bit would like to fix what is causing the error. The program code and the complete error is below. Thanks as always!

我正在完成学校的一项实验作业,当我编译时,我得到了这个错误。程序运行良好,bit希望修复导致错误的原因。程序代码和完整的错误如下。谢谢一如既往!

Error: Note: F:\Java\Lab 8\Lab8.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

错误:注意:8 \ Lab8 F:\ Java \实验室。java使用未检查的或不安全的操作。注意:用-Xlint重新编译:不检查细节。

Code:

代码:

   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import javax.swing.event.*;
   import javax.swing.border.*;


   public class Lab8 extends JFrame {
       public Lab8()
           {

           // Create an array of Strings for age ranges
           String[] ageRanges = {"Under 20", "20-29", "30-39", "40-49", "50-59", "60 and Above"};
           JComboBox jcbo = new JComboBox(ageRanges);

           // Create an array of String destinations
           String[] destination = {"Mercury", "Venus", "Moon", "Mars", "Jupiter / Europa", "Saturn / Triton", "Pluto + Sharon"};
           JList jlst = new JList();

           // Declare radio buttons
           JRadioButton jrbMonday, jrbTuesday, jrbWednesday, jrbThursday, jrbFriday;

           // Create a textfield
           JTextField jMsg = new JTextField(10);


           // Create panel to hold label and textbox.
           JPanel p1 = new JPanel();
           p1.setLayout(new BorderLayout(5,0));
           p1.add(new JLabel("Name: "), BorderLayout.WEST);
           p1.add(new JTextField(20), BorderLayout.CENTER);
           jMsg.setHorizontalAlignment(JTextField.LEFT);


           // Create combobox panel.
           JPanel p2 = new JPanel();
           p2.setLayout(new GridLayout(2,0,5,5));
           p2.add(p1, BorderLayout.NORTH);
           p2.add(new JComboBox(ageRanges), BorderLayout.CENTER);
               p2.setBorder(new TitledBorder("Passenger Name & Age Range"));


           //Create listbox panel.
           JPanel p3 = new JPanel();
           p3.setLayout(new GridLayout(1, 0));
           p3.add(new JList(destination));
               p3.setBorder(new TitledBorder("Destinations"));


           // Create a new panel to hold radio buttons.
               JPanel r1 = new JPanel();
           r1.setLayout(new GridLayout(3,2));
           r1.add(jrbMonday = new JRadioButton("Monday"));
           r1.add(jrbTuesday = new JRadioButton("Tuesday"));
           r1.add(jrbWednesday = new JRadioButton("Wednesday"));
           r1.add(jrbThursday = new JRadioButton("Thursday"));
           r1.add(jrbFriday = new JRadioButton("Friday"));
           r1.setBorder(new TitledBorder("Departure Days"));


           // Create a radio button group to group five buttons
           ButtonGroup group = new ButtonGroup();
           group.add(jrbMonday);
           group.add(jrbTuesday);
           group.add(jrbWednesday);
           group.add(jrbThursday);
           group.add(jrbFriday);


           // Create grid to hold contents
           JPanel pMain = new JPanel();
           pMain.setLayout(new BorderLayout(5,0));
           add(r1, BorderLayout.CENTER);
           add(p2, BorderLayout.NORTH);
           add(p3, BorderLayout. EAST);

}


public static void main(String[] args)
       {
           Lab8 frame = new Lab8();
           frame.pack();
           frame.setTitle("Lab 8 Application");
           frame.setLocationRelativeTo(null); // Center the frame
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.setSize(425, 275);
           frame.setVisible(true);
        }
}

2 个解决方案

#1


13  

What this means is that the Java compiler has noticed some potentially unsafe issues with your code and is warning you. These issues are normally very trivial and you could carry on with them; especially since this is school work. But to find the issues, you should compile again with: javac -Xlint:unchecked Lab8.java like the compiler says.

这意味着Java编译器已经注意到您的代码存在一些潜在的不安全问题,并警告您。这些问题通常都很琐碎,你可以继续;尤其是这是学校的工作。但是要找到问题,您应该重新编译:javac -Xlint:未选中的Lab8。java就像编译器说的那样。

The issues in this file are that you haven't specified the type of object the JComboBox and JList are dealing with. Since you are only dealing with Strings in the JComboBox and JList, you should specify that. Read up on Java generics and this for more information.

这个文件中的问题是,您没有指定JComboBox和JList处理的对象的类型。因为您只处理JComboBox和JList中的字符串,所以应该指定它。请阅读Java泛型和本文以获得更多信息。

Change

改变

String[] ageRanges = {"Under 20", "20-29", "30-39", "40-49", "50-59", "60 and Above"};
JComboBox jcbo = new JComboBox(ageRanges);

to

String[] ageRanges = {"Under 20", "20-29", "30-39", "40-49", "50-59", "60 and Above"};
JComboBox<String> jcbo = new JComboBox<String>(ageRanges);

Also change:

也发生了变化:

p2.add(new JComboBox(ageRanges), BorderLayout.CENTER);
p2.setBorder(new TitledBorder("Passenger Name & Age Range"));

to

p2.add(new JComboBox<String>(ageRanges), BorderLayout.CENTER);
p2.setBorder(new TitledBorder("Passenger Name & Age Range"));

Finally change

最后的改变

//Create listbox panel.
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1, 0));
p3.add(new JList(destination));

to

//Create listbox panel.
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1, 0));
p3.add(new JList<String>(destination));

Edit:

编辑:

Not recommended for production code, but to bypass these warnings use:

不建议用于生产代码,但要绕过这些警告使用:

@SuppressWarnings("unchecked") 

Just add this above any method that makes unsafe operations. For instance, I think you could put it above your main method in this code like so:

只要在任何不安全操作的方法上面添加这个。例如,我认为你可以把它放在你的主要方法上面,比如:

@SuppressWarnings("unchecked") 
public static void main(String[] args) {
...

This would suppress the warnings.

这将抑制警告。

#2


0  

Add generic parameter <String> for JComboBox and JList.

为JComboBox和JList添加通用参数

PS: use IDE with syntax highlighting. For example - JetBrains IDEA have free community edition.

PS:使用带有语法突出显示的IDE。例如,JetBrains有免费的社区版。

#1


13  

What this means is that the Java compiler has noticed some potentially unsafe issues with your code and is warning you. These issues are normally very trivial and you could carry on with them; especially since this is school work. But to find the issues, you should compile again with: javac -Xlint:unchecked Lab8.java like the compiler says.

这意味着Java编译器已经注意到您的代码存在一些潜在的不安全问题,并警告您。这些问题通常都很琐碎,你可以继续;尤其是这是学校的工作。但是要找到问题,您应该重新编译:javac -Xlint:未选中的Lab8。java就像编译器说的那样。

The issues in this file are that you haven't specified the type of object the JComboBox and JList are dealing with. Since you are only dealing with Strings in the JComboBox and JList, you should specify that. Read up on Java generics and this for more information.

这个文件中的问题是,您没有指定JComboBox和JList处理的对象的类型。因为您只处理JComboBox和JList中的字符串,所以应该指定它。请阅读Java泛型和本文以获得更多信息。

Change

改变

String[] ageRanges = {"Under 20", "20-29", "30-39", "40-49", "50-59", "60 and Above"};
JComboBox jcbo = new JComboBox(ageRanges);

to

String[] ageRanges = {"Under 20", "20-29", "30-39", "40-49", "50-59", "60 and Above"};
JComboBox<String> jcbo = new JComboBox<String>(ageRanges);

Also change:

也发生了变化:

p2.add(new JComboBox(ageRanges), BorderLayout.CENTER);
p2.setBorder(new TitledBorder("Passenger Name & Age Range"));

to

p2.add(new JComboBox<String>(ageRanges), BorderLayout.CENTER);
p2.setBorder(new TitledBorder("Passenger Name & Age Range"));

Finally change

最后的改变

//Create listbox panel.
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1, 0));
p3.add(new JList(destination));

to

//Create listbox panel.
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1, 0));
p3.add(new JList<String>(destination));

Edit:

编辑:

Not recommended for production code, but to bypass these warnings use:

不建议用于生产代码,但要绕过这些警告使用:

@SuppressWarnings("unchecked") 

Just add this above any method that makes unsafe operations. For instance, I think you could put it above your main method in this code like so:

只要在任何不安全操作的方法上面添加这个。例如,我认为你可以把它放在你的主要方法上面,比如:

@SuppressWarnings("unchecked") 
public static void main(String[] args) {
...

This would suppress the warnings.

这将抑制警告。

#2


0  

Add generic parameter <String> for JComboBox and JList.

为JComboBox和JList添加通用参数

PS: use IDE with syntax highlighting. For example - JetBrains IDEA have free community edition.

PS:使用带有语法突出显示的IDE。例如,JetBrains有免费的社区版。