图片+文字的JList

本文介绍如何在Java Swing的JList组件中显示带有图片和文字的列表项,通过自定义CellRenderer实现,并提供了一个完整的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

具体方法如下:
首先得实现一个接口:CellRenderer,这是第一步,也是最重要最关键的一部。代码不长,但不容易想到。

 

  1. package Demo;  
  2.   
  3. import java.awt.Component;  
  4.   
  5. import javax.swing.BorderFactory;  
  6. import javax.swing.Icon;  
  7. import javax.swing.JLabel;  
  8. import javax.swing.JList;  
  9. import javax.swing.ListCellRenderer;  
  10.   
  11. public class MyCellRenderer extends JLabel implements ListCellRenderer {  
  12.  Icon[] icons;  
  13.  public MyCellRenderer(){};  
  14.  public MyCellRenderer(Icon[] icons) {  
  15.   // TODO Auto-generated constructor stub  
  16.   this.icons=icons;  
  17.  }  
  18.  @Override  
  19.  public Component getListCellRendererComponent(JList list, Object value,  
  20.    int index, boolean isSelected, boolean cellHasFocus) {  
  21.   String s = value.toString();  
  22.   setText(s);  
  23.   setBorder(BorderFactory.createEmptyBorder(5555));//加入宽度为5的空白边框  
  24.   if (isSelected) {  
  25.    setBackground(list.getSelectionBackground());  
  26.    setForeground(list.getSelectionForeground());  
  27.   } else {  
  28.    setBackground(list.getBackground());  
  29.    setForeground(list.getForeground());  
  30.   }  
  31.   setIcon(icons[index]);//设置图片  
  32.   setEnabled(list.isEnabled());  
  33.   setFont(list.getFont());  
  34.   setOpaque(true);  
  35.   return this;  
  36.  }  
  37.   
  38. }  
   
然后你在使用JList时,如果想要可以显示图片+文字,只需要使用JList的setCellRenderer(new MyCellRenderer

(icons));方法就可以了,其中MyCellRenderer即是上面实现的类,icons是想要显示的图片的数组,其他的使用方法都

和普通的JList相同。
 

  1. package Demo;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.event.ActionEvent;  
  5. import java.awt.event.ActionListener;  
  6. import java.awt.event.WindowAdapter;  
  7. import java.awt.event.WindowEvent;  
  8. import javax.swing.DefaultListModel;  
  9. import javax.swing.Icon;  
  10. import javax.swing.ImageIcon;  
  11. import javax.swing.JButton;  
  12. import javax.swing.JFrame;  
  13. import javax.swing.JList;  
  14. import javax.swing.JScrollPane;  
  15. import javax.swing.ListSelectionModel;  
  16.   
  17. public class ListWithImgDemo extends JFrame implements ActionListener {  
  18.   
  19.  /** 
  20.   * @param args 
  21.   */  
  22.  //文字数组  
  23.  String[] data = { "好友1""好友2""好友3""好友4""好友5""好友6""好友7",  
  24.    "好友8""好友9""好友10""好友11" , "好友12" , "好友13" , "好友14"   
  25.    , "好友15" , "好友16" , "好友17" , "好友18" };  
  26.  Icon icon1 = new ImageIcon("img/1-1.gif");  
  27.  Icon icon2 = new ImageIcon("img/2-1.gif");  
  28.  Icon icon3 = new ImageIcon("img/3-1.gif");  
  29.  Icon icon4 = new ImageIcon("img/4-1.gif");  
  30.  Icon icon5 = new ImageIcon("img/5-1.gif");  
  31.  Icon icon6 = new ImageIcon("img/6-1.gif");  
  32.  Icon icon7 = new ImageIcon("img/7-1.gif");  
  33.  Icon icon8 = new ImageIcon("img/8-1.gif");  
  34.  Icon icon9 = new ImageIcon("img/9-1.gif");  
  35.  Icon icon10 = new ImageIcon("img/10-1.gif");  
  36.  Icon icon11 = new ImageIcon("img/11-1.gif");  
  37.  Icon icon12 = new ImageIcon("img/12-1.gif");  
  38.  Icon icon13 = new ImageIcon("img/13-1.gif");  
  39.  Icon icon14 = new ImageIcon("img/14-1.gif");  
  40.  Icon icon15 = new ImageIcon("img/15-1.gif");  
  41.  Icon icon16 = new ImageIcon("img/16-1.gif");  
  42.  Icon icon17 = new ImageIcon("img/17-1.gif");  
  43.  Icon icon18 = new ImageIcon("img/18-1.gif");  
  44.  //图片数组  
  45.  Icon[] icons = { icon1, icon2, icon3, icon4, icon5, icon6, icon7, icon8,  
  46.    icon9, icon10, icon11, icon12, icon13, icon14, icon15, icon16, icon17, icon18 };  
  47.  JList list;  
  48.  JButton add;  
  49.  JButton remove;  
  50.  JScrollPane listScroller;  
  51.  DefaultListModel listModel;  
  52.   
  53.  ListWithImgDemo() {  
  54.   listModel = new DefaultListModel();  
  55.   for(int i=0;i<data.length;i++){  
  56.    listModel.add(i, data[i]);  
  57.   }  
  58.   list = new JList(listModel);  
  59.   list.setCellRenderer(new MyCellRenderer(icons));//使用自己的CellRenderer  
  60.   list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);// 设置单一选择模式(每次只能  
  61.   
  62. 有一个元素被选中)  
  63.   listScroller = new JScrollPane(list);  
  64.   add = new JButton("Add");  
  65.   add.addActionListener(this);  
  66.   remove = new JButton("Remove");  
  67.   remove.addActionListener(this);  
  68.   add(listScroller,BorderLayout.CENTER);//添加带滚动条的list  
  69.   add(add,BorderLayout.NORTH);  
  70.   add(remove,BorderLayout.SOUTH);  
  71.   setSize(200500);  
  72.   addWindowListener(new WindowAdapter() {  
  73.    public void windowClosing(WindowEvent e) {  
  74.     dispose();  
  75.     System.exit(0);  
  76.    }  
  77.   });  
  78.  }  
  79.   
  80.  public static void main(String[] args) {  
  81.   ListWithImgDemo demo = new ListWithImgDemo();  
  82.   demo.setVisible(true);  
  83.  }  
  84.    
  85.  /** 
  86.   * Add与Remove按钮事件 
  87.   */  
  88.  public void actionPerformed(ActionEvent e) {  
  89.   // TODO Auto-generated method stub  
  90.   if (e.getSource() == add) {  
  91.    if (listModel.getSize() < data.length) {  
  92.     listModel.add(listModel.getSize(), data[listModel.getSize()]);  
  93.    }  
  94.   }  
  95.   if (e.getSource() == remove) {  
  96.    if (listModel.getSize() > 0) {  
  97.     listModel.remove(listModel.getSize() - 1);  
  98.    }  
  99.   }  
  100.  }  
  101.   
  102. }  

经过这次我体会到了文档的重要性,我用的是JDK 5.0 Documentation,的确是好东西,不光可以查看类有哪些方法,如

何使用,其实里面还有n多经典的demo,也许让你挠破头皮的问题,在里面就有很详细的解释。建议使用英文版的,因为

很多中文翻译的不全或者有歧义,容易让思路陷入歧途。所有的源码我都上传到优快云,下载地址:
http://download.youkuaiyun.com/source/1851718
运行效果如图。有兴趣的可以下来看看。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值