请问能在JComboBox里添加图片吗?如果能请给写下 请问能在JComboBox里添加图片吗?如果能请给写下 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 当然可以加JComboBox的addItem(Object obj)方法其中Obj可以是任何形式的对象,你可以把ImageIcon对象加到列表里 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; }} 怎样获取真正的字符串宽度 关于使用dom4j 注释xml文件中某个节点问题!谢谢各位 大家好,这段代码我看不太懂,请大家指点一下 学Java还是学.Net有前途! 关于内部类的问题 请问关于JList的valueChanged的问题 Frame2中如何调用Frame1中的方法或者函数? 初学者求助!急! 如何把junit、jtest加到jcreator(在jcreator里可以import)? java接口的问题 java写文本文件的问题! 新手请教:JAVA编程思想中的一个匿名类的例子,调试出错。谢谢。
JComboBox的addItem(Object obj)方法
其中Obj可以是任何形式的对象,你可以把ImageIcon对象加到列表里
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;
}
}