Java JComboBox.setSelectedItem与null的行为不同

Java JComboBox.setSelectedItem与null的行为不同,java,swing,null,jcombobox,Java,Swing,Null,Jcombobox,按原样运行程序,并注释掉这两行 // comboBox.setSelectedItem(selected1); // comboBox.setSelectedItem(new Item(null,null)); 它将生成一个包含3个项目的组合框,默认情况下显示第一个项目 删除以下行中的注释 comboBox.setSelectedItem(selected1); 它将生成一个包含3项的组合框,但显示的第一项为空。基本上没有默认项,因为它正在搜索空项,但没有找到空项 将注释

按原样运行程序,并注释掉这两行

//      comboBox.setSelectedItem(selected1);
//      comboBox.setSelectedItem(new Item(null,null));
它将生成一个包含3个项目的组合框,默认情况下显示第一个项目

删除以下行中的注释

comboBox.setSelectedItem(selected1);
它将生成一个包含3项的组合框,但显示的第一项为空。基本上没有默认项,因为它正在搜索空项,但没有找到空项

将注释添加回下一行:

//      comboBox.setSelectedItem(selected1);
并从下一行中删除

comboBox.setSelectedItem(new Item(null,null));
它将生成一个包含3个项目的组合框,默认情况下显示第一个项目

两个setSelectedItem()调用都搜索不存在但最终结果不同的项。在一个场景中,默认选定项为空,而在另一个场景中,默认选定项为第一个

不同行为的原因是什么

import java.awt.Graphics;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class JComboBoxDefaultSelection 
{

    public static void main(String[] args) 
    {
        Object selected1 = null;
        Item selected2 = null;
        JComboBox<Item> comboBox = new JComboBox<Item>(new Item[] {
            new Item("Major", "red"), new Item("Critical", "dark"),
            new Item("Minor", "green") });
        JFrame frame = new JFrame();
        frame.add(comboBox);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
//      comboBox.setSelectedItem(selected1);
//      comboBox.setSelectedItem(new Item(null,null));
    }

}

class Item {

      private String name;
      private String color;

      public Item(String name, String color) {
        this.name = name;
        this.color = color;
      }

      public String getName() {
        return name;
      }

      public void setName(String name) {
        this.name = name;
      }

      public String getColor() {
        return color;
      }

      public void setColor(String color) {
        this.color = color;
      }

      @Override
      public String toString() {
        return name;
      }
    }
导入java.awt.Graphics;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
公共类JComboxDefaultSelection
{
公共静态void main(字符串[]args)
{
对象selected1=null;
所选项目2=空;
JComboBox组合框=新JComboBox(新项[]{
新项目(“主要”、“红色”)、新项目(“关键”、“深色”),
新项目(“次要”、“绿色”)};
JFrame=新JFrame();
frame.add(组合框);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//comboBox.setSelectedItem(selected1);
//comboBox.setSelectedItem(新项(null,null));
}
}
类项目{
私有字符串名称;
私有字符串颜色;
公共项(字符串名称、字符串颜色){
this.name=名称;
这个颜色=颜色;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getColor(){
返回颜色;
}
公共void setColor(字符串颜色){
这个颜色=颜色;
}
@凌驾
公共字符串toString(){
返回名称;
}
}

Cause:specification-说明文件中指出:“如果某个对象不在列表中且组合框不可编辑,则不会更改当前选择……参数:anObject-要选择的列表对象;使用null清除选择”列表中没有任何对象,因此它们的行为不应该相同吗?或者是只有第一个对象为空并且清除选择的问题。虽然第二个对象不在列表中,但它不是空对象。我想这意味着默认设置是选择列表中的第一项???你没有全部阅读吗?????实际上,我已经用粗体标记了它:“使用
null
清除选择”,因为
objectselected1=null
调用
组合框时使用的是
null
。setSelectedItem(selected1)
,因此选择被清除(或在编写时为“空白”!顺便说一句,
newitem(null,null)
不是
null
,尽管它满了(它的字段是
null
)-它正在被搜索,但在项目列表中找不到,所以选择没有改变(停留在第一个项目,创建后的默认值),只是想确认我的理解。谢谢你的回复。