请教JComboBox的内容为 Icon + String 应该怎么做?

解决方案 »

  1.   

    Vector installDatas = new Vector();
    installDatas.add(new InstallData("a", new ImageIcon("a.png")));
    comboBox.setModel(new DefaultComboBoxModel(installDatas));
    comboBox.setRenderer(new ComboBoxCellRenderer());
    引用如下类
    public class ComboBoxCellRenderer
    extends JLabel
    implements ListCellRenderer
    {
    public ComboBoxCellRenderer()
    {
    setOpaque(true);
    }public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
    {
    InstallData installData = (InstallData) value;
    setText(installData.getName());
    setIcon(installData.getIcon());
    setFont(list.getFont());
    setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
    setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());
    return this;
    }
    }public class InstallData
    {private String name;
    private Icon icon;
    private Object object;
    private boolean selected;public InstallData(String name, boolean selected)
    {
    this.name = name;
    this.selected = selected;
    }public InstallData(String name, Icon icon)
    {
    this.name = name;
    this.icon = icon;
    }public InstallData(String name, Icon icon, boolean selected)
    {
    this.name = name;
    this.icon = icon;
    this.selected = selected;
    }public InstallData(String name, Icon icon, Object object)
    {
    this.name = name;
    this.icon = icon;
    this.object = object;
    }public InstallData(String name, Icon icon, Object object, boolean selected)
    {
    this.name = name;
    this.icon = icon;
    this.object = object;
    this.selected = selected;
    }public InstallData(Icon icon)
    {
    this.icon = icon;
    }public String getName()
    {
    return name;
    }public Icon getIcon()
    {
    return icon;
    }public Object getObject()
    {
    return object;
    }public boolean isSelected()
    {
    return selected;
    }public void setSelected(boolean selected)
    {
    this.selected = selected;
    }
    }