当前位置: 代码迷 >> J2SE >> 哪位高手能给小弟我个纯图片选择的JCombobox的源代码,收到立即给分
  详细解决方案

哪位高手能给小弟我个纯图片选择的JCombobox的源代码,收到立即给分

热度:122   发布时间:2016-04-24 13:26:52.0
谁能给我个纯图片选择的JCombobox的源代码,收到立即给分!
我现在需要一个纯图片选择的JCombobox的源代码,类似像Word中的字体设置窗体中颜色选择那样,请不吝赐教谢谢!

------解决方案--------------------
利用JCombobox.setRenderer()设置自己的ListCellRenderer, 一个ListCellRenderer的框架:

class MyCellRenderer JComponent implements ListCellRenderer {

public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
 
Graphics g = getGraphics();
g.drawImage( img, 0, 0, null );
return this;
}

private Image getImageOf( Object val ) {
...
return img;
}
 }


------解决方案--------------------
public class Test extends JFrame {

Test(){
JComboBox box=new JComboBox(new String[]{"C:/Documents and Settings/d/桌面/Greenstone.gif","C:/Documents and Settings/d/桌面/Gone Fishing.gif"});
box.setRenderer(new CellRendererClass());
add(box);
setSize(100, 80);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new Test();
}
}

class CellRendererClass extends JLabel implements ListCellRenderer{
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
setIcon(new ImageIcon(value.toString()));
return this;
}
}