请问能在JComboBox里添加图片吗?如果能请给写下

解决方案 »

  1.   

    当然可以加
    JComboBox的addItem(Object obj)方法
    其中Obj可以是任何形式的对象,你可以把ImageIcon对象加到列表里
      

  2.   

    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;
    }
    }