5

我正在尝试从 Liang 的“Java 编程简介第 9 版”中编译以下程序。以下关于 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
4

3 回答 3

15

这是警告而不是错误。您缺少Java 1.7 中引入的JComboBox所期望的泛型类型。如果没有它,每次从将类型ComboBoxModel 添加String到声明中检索值以匹配模型数据时,都需要进行强制转换

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

阅读这篇有趣的文章什么是“未经检查”的警告?来自泛型常见问题解答

于 2013-08-04T23:05:32.967 回答
0

截至目前,Oracle 在他们的 Swing 教程中有:

https://docs.oracle.com/javase/tutorial/uiswing/examples/layout/CardLayoutDemoProject/src/layout/CardLayoutDemo.java

运行此命令会导致以下行引发此警告:

JComboBox cb = new JComboBox(comboBoxItems);

可能是因为代码自 2008 年以来没有更新?这个解决方案帮助我清理了警告并顺利编译,非常感谢。

于 2019-10-18T10:24:27.527 回答
0

这是警告而不是错误,因为imageComboBox = new JComboBox( names );

再次运行程序,它将正常工作

这是运行的图像

于 2020-02-13T12:17:22.560 回答