jCombox的列表里,怎样加图片item。 如题。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 怎么,没有找到类似重新编辑帖子的地方。JCombox列表的Item是图片的话,怎么实现,用的是Swing。有高手路走,请指点迷津, 关于分数的问题第一次用,不会弄,发现给的太少了。。下次就狂给分。 找到一份答案。//使用的代码 picture=new JComboBox(); picture.setBounds(new Rectangle(283,81,58,37));//没使用Layout,这是定位它的位置和大小 String[] picArr=new String[]{ "pic1.jpg ", "pic2.jpg ", "pic3.jpg ", "pic4.jpg "};//Combox中显示的图片名 picture.setModel(new HeadPicComboBoxModel(picArr));//设置ComboBox中元素模型 picture.setRenderer(new HeadpicCellRenderer()); //设置ComboBox的显示 //继承DefaultComboBoxModel设置要在ComboBox中显示的元素模型类 class HeadPicComboBoxModel extends DefaultComboBoxModel { public HeadPicComboBoxModel(String[] pics) { for(int i=0; i < pics.length; ++i) { //添加显示的对象 addElement(new Object[] { new ImageIcon( "images\\ "+pics[i]) } ); } } //在HeadpicCellRenderer中要用到 public Icon getIcon(Object object) { Object[] array = (Object[])object; return (Icon)array[0]; } } //ComboBox显示方式 class HeadpicCellRenderer extends JLabel implements ListCellRenderer { private Border lineBorder = BorderFactory.createLineBorder(Color.red, 2), emptyBorder = BorderFactory.createEmptyBorder(2,2,2,2); public HeadpicCellRenderer() { setOpaque(true); } public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus) { HeadPicComboBoxModel model =(HeadPicComboBoxModel)list.getModel(); setIcon(model.getIcon(value)); if(isSelected) { setForeground(list.getSelectionForeground()); setBackground(list.getSelectionBackground()); } else { setForeground(list.getForeground()); setBackground(list.getBackground()); } if(cellHasFocus) setBorder(lineBorder); else setBorder(emptyBorder); return this; } } ImageIcon img = new ImageIcon("D:\\ImageLS\\ls\\name.jpg");buttonUp = new JButton(img); 谢了,不过说的是,combox。不是button。 public class JComboBoxTest { JPanel panel; public void initial() { panel = new JPanel(); JComboBox comboBox = new JComboBox(); comboBox.setRenderer(new customListCellRenderer()); comboBox.addItem("aaa"); panel.add(comboBox); } public static void main(String[] args) { JFrame frame = new JFrame("IndexPanel"); JComboBoxTest test= new JComboBoxTest(); test.initial(); frame.setContentPane(test.panel); frame.setPreferredSize(new Dimension(300,300)); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.pack(); frame.setVisible(true); } class customListCellRenderer implements ListCellRenderer{ public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { return new JLabel(new ImageIcon(this.getClass().getResource("hide.jpg"))); } }} 总是copy别人的代码是很难提高的在swing里几乎所有控件都有三部分组成,控件类(JXXX),模型类(XXXModel),渲染器(XXXRenderer)所以,想要定制的显示,就要实现一个XXXRenderer,然后对控件调用setXXCellRenderer就OK了要是想存储定制的数据类型,就要实现自己的XXXModel思路说到这里,具体实现自己研究,然后回顾一下,才会不断提高 import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.util.*;class JComboBoxTest { JFrame mainFrame; JComboBox simpleComboBox; public JComboBoxTest() { mainFrame = new JFrame ( "JComboBoxDemo" ); Vector<String> cbData = new Vector<String>(); cbData.add("images/a.jpg"); cbData.add("images/b.jpg"); cbData.add("images/c.jpg"); cbData.add("images/d.jpg"); simpleComboBox = new JComboBox( cbData); simpleComboBox.setPreferredSize( new Dimension(200,130) ); simpleComboBox.setMaximumRowCount(3); simpleComboBox.setRenderer( new CustomComboBoxRenderer() ); mainFrame.getContentPane().add( simpleComboBox ); simpleComboBox.setCursor( new Cursor(Cursor.HAND_CURSOR) ); mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); mainFrame.pack(); mainFrame.setLocationRelativeTo(null); mainFrame.setVisible( true ); } public static void main(String[] args) { new JComboBoxTest(); } class CustomComboBoxRenderer extends JLabel implements ListCellRenderer{ CustomComboBoxRenderer(){ setOpaque(true); setHorizontalAlignment(CENTER); setVerticalAlignment(CENTER); } public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } String imageFileName = (String)value; ImageIcon labelIcon = new ImageIcon(getClass().getResource(imageFileName)); setText( imageFileName.substring(imageFileName.lastIndexOf('/')+1) ); setIcon( labelIcon ); return this; } }} 问题的解决主要是重新写他的渲染器,上面的是我弄成功的。其中注意的两个问题是 ImageIcon labelIcon = new ImageIcon(getClass().getResource(imageFileName)); 有好多找到的程序都是这样写ImageIcon labelIcon = new ImageIcon(imageFileName); 在调试的过程中发现出不来结果。imageFileName的应该是“包名/图片名(gif,jpg等)图片的上层没有包,直接写图片名 为什么在同一个包里面对一个public 类编译时提示找不到符号 DataoutputStream乱码.... 文件路径问题 java调用perl 程序的一个问题 请问:jdk是向下兼容的吗? jcreator 启动调试器时出错? 一个数组中如何找到 分别在这个数组里面占多少个元素的算法 小弟实习问题 请教各位大虾 我刚做的一个共享程序,主要的部分都已经完成了,就差一个多线程下载,请各位高手帮帮忙呀. 是高手的進來!不是的不好意思!!在線等 大家能帮我看看以下的程序,一个登陆按钮,但就登陆不上去! jdbc操作数据库的问题,请大家帮忙提点意见
JCombox列表的Item是图片的话,怎么实现,用的是Swing。
有高手路走,请指点迷津, 关于分数的问题第一次用,不会弄,发现给的太少了。。
下次就狂给分。
//使用的代码
picture=new JComboBox(); picture.setBounds(new Rectangle(283,81,58,37));//没使用Layout,这是定位它的位置和大小
String[] picArr=new String[]{ "pic1.jpg ", "pic2.jpg ",
"pic3.jpg ", "pic4.jpg "};//Combox中显示的图片名
picture.setModel(new HeadPicComboBoxModel(picArr));//设置ComboBox中元素模型
picture.setRenderer(new HeadpicCellRenderer()); //设置ComboBox的显示
//继承DefaultComboBoxModel设置要在ComboBox中显示的元素模型类
class HeadPicComboBoxModel extends DefaultComboBoxModel {
public HeadPicComboBoxModel(String[] pics) {
for(int i=0; i < pics.length; ++i) {
//添加显示的对象
addElement(new Object[] { new ImageIcon( "images\\ "+pics[i]) } );
}
}
//在HeadpicCellRenderer中要用到
public Icon getIcon(Object object) {
Object[] array = (Object[])object;
return (Icon)array[0];
}
} //ComboBox显示方式
class HeadpicCellRenderer extends JLabel implements ListCellRenderer {
private Border
lineBorder = BorderFactory.createLineBorder(Color.red, 2),
emptyBorder = BorderFactory.createEmptyBorder(2,2,2,2); public HeadpicCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus) {
HeadPicComboBoxModel model =(HeadPicComboBoxModel)list.getModel(); setIcon(model.getIcon(value)); if(isSelected) {
setForeground(list.getSelectionForeground());
setBackground(list.getSelectionBackground());
}
else {
setForeground(list.getForeground());
setBackground(list.getBackground());
} if(cellHasFocus) setBorder(lineBorder);
else setBorder(emptyBorder); return this;
}
}
buttonUp = new JButton(img);
谢了,不过说的是,combox。不是button。
public class JComboBoxTest {
JPanel panel;
public void initial() {
panel = new JPanel();
JComboBox comboBox = new JComboBox();
comboBox.setRenderer(new customListCellRenderer());
comboBox.addItem("aaa");
panel.add(comboBox);
}
public static void main(String[] args) {
JFrame frame = new JFrame("IndexPanel");
JComboBoxTest test= new JComboBoxTest();
test.initial();
frame.setContentPane(test.panel);
frame.setPreferredSize(new Dimension(300,300));
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
class customListCellRenderer implements ListCellRenderer{
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
return new JLabel(new ImageIcon(this.getClass().getResource("hide.jpg")));
}
}
}
在swing里几乎所有控件都有三部分组成,控件类(JXXX),模型类(XXXModel),渲染器(XXXRenderer)
所以,想要定制的显示,就要实现一个XXXRenderer,然后对控件调用setXXCellRenderer就OK了
要是想存储定制的数据类型,就要实现自己的XXXModel
思路说到这里,具体实现自己研究,然后回顾一下,才会不断提高
import javax.swing.*;
import java.awt.event.*;
import java.util.*;class JComboBoxTest
{
JFrame mainFrame;
JComboBox simpleComboBox;
public JComboBoxTest() {
mainFrame = new JFrame ( "JComboBoxDemo" );
Vector<String> cbData = new Vector<String>();
cbData.add("images/a.jpg");
cbData.add("images/b.jpg");
cbData.add("images/c.jpg");
cbData.add("images/d.jpg");
simpleComboBox = new JComboBox( cbData);
simpleComboBox.setPreferredSize( new Dimension(200,130) );
simpleComboBox.setMaximumRowCount(3);
simpleComboBox.setRenderer( new CustomComboBoxRenderer() );
mainFrame.getContentPane().add( simpleComboBox ); simpleComboBox.setCursor( new Cursor(Cursor.HAND_CURSOR) );
mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
mainFrame.pack();
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible( true );
} public static void main(String[] args)
{
new JComboBoxTest();
} class CustomComboBoxRenderer extends JLabel implements ListCellRenderer{
CustomComboBoxRenderer(){
setOpaque(true);
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER); }
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
String imageFileName = (String)value;
ImageIcon labelIcon = new ImageIcon(getClass().getResource(imageFileName));
setText( imageFileName.substring(imageFileName.lastIndexOf('/')+1) );
setIcon( labelIcon );
return this;
}
}
}
其中注意的两个问题是
ImageIcon labelIcon = new ImageIcon(getClass().getResource(imageFileName));
有好多找到的程序都是这样写
ImageIcon labelIcon = new ImageIcon(imageFileName);
在调试的过程中发现出不来结果。
imageFileName的应该是“包名/图片名(gif,jpg等)图片的上层没有包,直接写图片名