我正在完成学校的实验室作业,并在编译时遇到此错误。程序运行正常,有点想修复导致错误的原因。程序代码和完整错误如下。一如既往地谢谢!
错误: 注意:F:\ Java \ Lab 8 \ Lab8.java使用未经检查或不安全的操作。 注意:使用-Xlint重新编译:取消选中以获取详细信息。
代码:
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);
}
}
答案 0 :(得分:13)
这意味着Java编译器已经注意到代码可能存在一些不安全的问题并且警告您。这些问题通常非常简单,你可以继续使用它们;特别是因为这是学校的工作。但是要找到问题,你应该再次编译:{{1}},就像编译器说的那样。
此文件中的问题是您尚未指定JComboBox和JList正在处理的对象类型。由于您只处理JComboBox和JList中的字符串,因此您应该指定它。请阅读Java generics和this以获取更多信息。
更改
javac -Xlint:unchecked Lab8.java
到
String[] ageRanges = {"Under 20", "20-29", "30-39", "40-49", "50-59", "60 and Above"};
JComboBox jcbo = new JComboBox(ageRanges);
同样改变:
String[] ageRanges = {"Under 20", "20-29", "30-39", "40-49", "50-59", "60 and Above"};
JComboBox<String> jcbo = new JComboBox<String>(ageRanges);
到
p2.add(new JComboBox(ageRanges), BorderLayout.CENTER);
p2.setBorder(new TitledBorder("Passenger Name & Age Range"));
最后改变
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));
编辑:
不建议用于生产代码,但要绕过这些警告,请使用:
//Create listbox panel.
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1, 0));
p3.add(new JList<String>(destination));
只需在上面添加任何使不安全操作的方法。例如,我认为你可以把它放在你的main方法上面,就像这样:
@SuppressWarnings("unchecked")
这会抑制警告。
答案 1 :(得分:0)
添加通用参数&lt; String&gt;对于JComboBox和JList。
PS:使用带语法高亮的IDE。例如 - JetBrains IDEA有免费社区版。