English 中文(简体)
java的无节制或不安全作业错误汇编? [复制]
原标题:Unchecked or unsafe operations error in java compile? [duplicate]
  • 时间:2012-04-14 17:44:40
  •  标签:
  • java

我正在完成学校的实验室任务,并在我汇编时发现这一错误。 该方案实行罚款,比方希望确定造成错误的原因。 方案代码和全部错误如下。 感谢!

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

法典:

   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);
        }
}
最佳回答

这意味着, Java汇编者已注意到一些可能不安全的问题,并警告你。 这些问题通常非常棘手,你可以继续处理,特别是因为这是学校的工作。 但是,为了找到这些问题,正如汇编者所说的那样,你应当再次汇编如下:javac-Xlint:unchecked Lab8.java

档案中的问题是,你没有具体说明JComboBox和JList正在处理的物体类型。 由于你只是处理JComboBox和JList的强项,你应具体说明这一点。 阅读 rel=“noretinger”>Java generals .this

变化

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

页: 1

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"));

页: 1

p2.add(new JComboBox<String>(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));

页: 1

//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 页: 1bypass these warnings use:

@SuppressWarnings("unchecked") 

仅此补充任何造成不安全行动的方法。 例如,我认为你可以把它放在本法典的主要方法之上:

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

这将压制警告。

问题回答

添加通用参数和斜体; 用于JComboBox和JList。

PS:利用IDE, 着重分析。 例如:JetBrains IDEA。 免费社区版。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签