2013-08-04 24 views
1

我试图让下面的程序从“Java程序设计第9版,由亮我越来越用下面的例子错误的问候JComboBox时编译:的JComboBox错误

import javax.swing.*; 

public class GUIComponents 
{ 
    public static void main (String[] args) 
    { 
     JButton jbtOK = new JButton ("OK");            // Creates a button with test OK 
     JButton jbtCancel = new JButton ("Cancel");         // Creats a cancel button 
     JLabel jlblName = new JLabel ("Enter your name: ");     // Creates a label with the respective text 
     JTextField jtfName = new JTextField ("Type Name Here");  // Creates a text field with the respective text 
     JCheckBox jchkBold = new JCheckBox ("Bold");      // Creates a check boc wth the text bold 
     JCheckBox jchkItalic = new JCheckBox ("Italic"); 
     JRadioButton jrbYellow = new JRadioButton ("Yellow");    // Creates a radio button with text Yellow 
     JRadioButton jrbRed = new JRadioButton ("Red");      // Creates a radio Button with text Red 
     **JComboBox jcboColor = new JComboBox (new String[] {"Freshman", "Sophomore", "Junior", "Senior"});** 
     JPanel panel = new JPanel();               // Creates a panel to group components 
     panel.add (jbtOK);                   // Add the OK button to the panel 
     panel.add (jbtCancel);                  // Add the Cancel button to the panel 
     panel.add (jlblName);                  // Add the lable to the panel 
     panel.add (jtfName); 
     panel.add (jchkBold); 
     panel.add (jchkItalic); 
     panel.add (jrbRed); 
     panel.add (jrbYellow); 
     panel.add (jcboColor); 

     JFrame frame = new JFrame(); 
     frame.add (panel); 
     frame.setTitle ("Show GUI Components"); 
     frame.setSize (450,100); 
     frame.setLocation (200, 100); 
     frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
     frame.setVisible (true); 
    } 
} 

正在生产的错误是:。

warning: [unchecked] unchecked call to JComboBox(E[]) as a member of the raw type JComboBox 
    JcomboBox jcboColor = new JComboBox(new String[] {"Freshman", "Sophomore", "Junior", "Senior"}); 

Where E is a time-variable: 
    E extends Object Declared in class JComboBox 
+0

你肯定有一个错误这个有趣的文章What is an "unchecked" warning??这听起来像是关于类型安全的警告。如果确实如此,那么你可以使用@SuppressWarnings(“unchecked”),如下所示:http://stackoverflow.com/questions/1129795/what-is-suppresswarnings-unchecked-in-java –

回答

7

这是不是一个错误你错过了泛型类型的JComboBox预计,在Java 1.7推出的警告没有它铸造每次值将是必要的从ComboBoxModel 0123中检索添加String类型的声明相匹配的模型数据

JComboBox<String> jcboColor = new JComboBox<>(new String[] { ... }); 

阅读来自仿制药FAQ