我写了一个带图标的JComboBox.....
希望对你有用..
import javax.swing.*;
import java.awt.*;
import javax.swing.border.LineBorder;
public class IconRenderer extends JLabel implements ListCellRenderer{
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
Object[] cell = (Object[]) value;
this.setIcon((Icon) cell[0]);
this.setText(cell[1].toString());
this.setToolTipText(cell[2].toString());
this.setBorder(new LineBorder(Color.white));
if(isSelected)
this.setForeground(Color.magenta);
else
this.setForeground(list.getForeground());
return this;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class IconComboBoxDemo extends JFrame{
private JComboBox iconComboBox;
public IconComboBoxDemo(){
super("带图标的ComboBox");
Object[][] obj = {{new ImageIcon("1.gif"),"aaaa","aaaaa"},
{new ImageIcon("2.gif"),"cccc","bbbbb"},
{new ImageIcon("3.gif"),"adfefa","aetggf"}
};
iconComboBox = new JComboBox();
iconComboBox.setMaximumRowCount(3);
iconComboBox.setRenderer(new IconRenderer());
for(int i =0;i<obj.length;i++){
iconComboBox.addItem(obj[i]);
}
this.getContentPane().add(iconComboBox,BorderLayout.NORTH);
this.setSize(300,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new IconComboBoxDemo();
}
}