JComboBox默认是不支持显示图片的,这就得自己动手重写一个JComboBox,主要重写它的UI 和ListRederer
1、addItem的显示重写,
/**
*
*/
private static final long serialVersionUID = -4164259284389215412L;
private String path;
public ComboBoxImageItem(String path){
this.path = path;
this.setOpaque(true);
}
/**
* list - 正在绘制的 JList。
* value - 由 list.getModel().getElementAt(index) 返回的值。
* index - 单元格索引。
* isSelected - 如果选择了指定的单元格,则为 true。
* cellHasFocus - 如果指定的单元格拥有焦点,则为 true。
*/
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if(value!=null)
{
setIcon(new ImageIcon(path + value));
setBackground(isSelected ? Color.GRAY : Color.WHITE);
setForeground(isSelected ? Color.WHITE : Color.GRAY);
}
return this;
}
}
2、重写JComboBox的UI
@Override
public void paint(Graphics g, JComponent c) {
hasFocus = comboBox.hasFocus();
if ( !comboBox.isEditable() ) {
Rectangle r = rectangleForCurrentValue();
paintCurrentValueBackground(g,r,hasFocus);
paintCurrentValue(g,r,hasFocus);
}
}
/**
* 重写JComboBox的按钮
*/
@Override
protected JButton createArrowButton() {
String path = "C:/Documents and Settings/Administrator/workspace/IQBoard/src/com/returnstar/board/resource/img/toolbar/draw/h.png";
JButton button = new JButton(new ImageIcon(path));
button.setName("ComboBox.arrowButton");
return button;
}
@Override
protected Rectangle rectangleForCurrentValue() {
int width = comboBox.getWidth();
int height = comboBox.getHeight();
Insets insets = getInsets();
int editorWidth = width - (insets.left + insets.right);
int editorHeight = (height - (insets.top + insets.bottom - 1)) / 2;
return new Rectangle(
insets.left, insets.top, editorWidth, editorHeight);
}
@Override
protected LayoutManager createLayoutManager() {
return new LayoutM();
}
class LayoutM implements LayoutManager{
public void addLayoutComponent(String name, Component comp) {}
public void layoutContainer(Container parent) {
JComboBox box = (JComboBox) parent;
int width = box.getWidth();
int height = box.getHeight();
Insets insets = getInsets();
int buttonWidth = width - (insets.left + insets.right);
int buttonHeight = (height - (insets.top + insets.bottom + 2)) / 2;
if (arrowButton != null){
arrowButton.setBounds(
insets.left, height - insets.bottom - buttonHeight,
buttonWidth, buttonHeight);
}
if (editor != null){
editor.setBounds(rectangleForCurrentValue());
}
}
public Dimension minimumLayoutSize(Container parent) {
return parent.getMinimumSize();
}
public Dimension preferredLayoutSize(Container parent) {
return parent.getPreferredSize();
}
public void removeLayoutComponent(Component comp) {}
}
}
3、CustomComboBox自定义按钮
/**
*
*/
private static final long serialVersionUID = -1255101315734281668L;
public CustomComboBox(){
this.setUI(new ComboBoxExtendsUI());
String path = "C:/Documents and Settings/Administrator/workspace/IQBoard/src/com/returnstar/board/resource/img/toolbar/draw/";
ComboBoxImageItem label = new ComboBoxImageItem(path);
this.setRenderer(label);
}
}
4、main函数
public static void main(String[] args) {
JFrame jframe = new JFrame();
jframe.setLayout(null);
CustomComboBox combo = new CustomComboBox();
combo.addItem("streak/1.png");
combo.addItem("streak/2.png");
combo.addItem("streak/3.png");
combo.addItem("streak/4.png");
combo.addItem("streak/5.png");
combo.setBounds(100, 100, 60, 30);
jframe.add(combo);
jframe.setBounds(300, 300, 500, 400);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
}