具体方法如下:
首先得实现一个接口:CellRenderer,这是第一步,也是最重要最关键的一部。代码不长,但不容易想到。
然后你在使用JList时,如果想要可以显示图片+文字,只需要使用JList的setCellRenderer(new MyCellRenderer
首先得实现一个接口:CellRenderer,这是第一步,也是最重要最关键的一部。代码不长,但不容易想到。
- package Demo;
- import java.awt.Component;
- import javax.swing.BorderFactory;
- import javax.swing.Icon;
- import javax.swing.JLabel;
- import javax.swing.JList;
- import javax.swing.ListCellRenderer;
- public class MyCellRenderer extends JLabel implements ListCellRenderer {
- Icon[] icons;
- public MyCellRenderer(){};
- public MyCellRenderer(Icon[] icons) {
- // TODO Auto-generated constructor stub
- this.icons=icons;
- }
- @Override
- public Component getListCellRendererComponent(JList list, Object value,
- int index, boolean isSelected, boolean cellHasFocus) {
- String s = value.toString();
- setText(s);
- setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));//加入宽度为5的空白边框
- if (isSelected) {
- setBackground(list.getSelectionBackground());
- setForeground(list.getSelectionForeground());
- } else {
- setBackground(list.getBackground());
- setForeground(list.getForeground());
- }
- setIcon(icons[index]);//设置图片
- setEnabled(list.isEnabled());
- setFont(list.getFont());
- setOpaque(true);
- return this;
- }
- }
然后你在使用JList时,如果想要可以显示图片+文字,只需要使用JList的setCellRenderer(new MyCellRenderer
(icons));方法就可以了,其中MyCellRenderer即是上面实现的类,icons是想要显示的图片的数组,其他的使用方法都
和普通的JList相同。
- package Demo;
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import javax.swing.DefaultListModel;
- import javax.swing.Icon;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JList;
- import javax.swing.JScrollPane;
- import javax.swing.ListSelectionModel;
- public class ListWithImgDemo extends JFrame implements ActionListener {
- /**
- * @param args
- */
- //文字数组
- String[] data = { "好友1", "好友2", "好友3", "好友4", "好友5", "好友6", "好友7",
- "好友8", "好友9", "好友10", "好友11" , "好友12" , "好友13" , "好友14"
- , "好友15" , "好友16" , "好友17" , "好友18" };
- Icon icon1 = new ImageIcon("img/1-1.gif");
- Icon icon2 = new ImageIcon("img/2-1.gif");
- Icon icon3 = new ImageIcon("img/3-1.gif");
- Icon icon4 = new ImageIcon("img/4-1.gif");
- Icon icon5 = new ImageIcon("img/5-1.gif");
- Icon icon6 = new ImageIcon("img/6-1.gif");
- Icon icon7 = new ImageIcon("img/7-1.gif");
- Icon icon8 = new ImageIcon("img/8-1.gif");
- Icon icon9 = new ImageIcon("img/9-1.gif");
- Icon icon10 = new ImageIcon("img/10-1.gif");
- Icon icon11 = new ImageIcon("img/11-1.gif");
- Icon icon12 = new ImageIcon("img/12-1.gif");
- Icon icon13 = new ImageIcon("img/13-1.gif");
- Icon icon14 = new ImageIcon("img/14-1.gif");
- Icon icon15 = new ImageIcon("img/15-1.gif");
- Icon icon16 = new ImageIcon("img/16-1.gif");
- Icon icon17 = new ImageIcon("img/17-1.gif");
- Icon icon18 = new ImageIcon("img/18-1.gif");
- //图片数组
- Icon[] icons = { icon1, icon2, icon3, icon4, icon5, icon6, icon7, icon8,
- icon9, icon10, icon11, icon12, icon13, icon14, icon15, icon16, icon17, icon18 };
- JList list;
- JButton add;
- JButton remove;
- JScrollPane listScroller;
- DefaultListModel listModel;
- ListWithImgDemo() {
- listModel = new DefaultListModel();
- for(int i=0;i<data.length;i++){
- listModel.add(i, data[i]);
- }
- list = new JList(listModel);
- list.setCellRenderer(new MyCellRenderer(icons));//使用自己的CellRenderer
- list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);// 设置单一选择模式(每次只能
- 有一个元素被选中)
- listScroller = new JScrollPane(list);
- add = new JButton("Add");
- add.addActionListener(this);
- remove = new JButton("Remove");
- remove.addActionListener(this);
- add(listScroller,BorderLayout.CENTER);//添加带滚动条的list
- add(add,BorderLayout.NORTH);
- add(remove,BorderLayout.SOUTH);
- setSize(200, 500);
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- dispose();
- System.exit(0);
- }
- });
- }
- public static void main(String[] args) {
- ListWithImgDemo demo = new ListWithImgDemo();
- demo.setVisible(true);
- }
- /**
- * Add与Remove按钮事件
- */
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- if (e.getSource() == add) {
- if (listModel.getSize() < data.length) {
- listModel.add(listModel.getSize(), data[listModel.getSize()]);
- }
- }
- if (e.getSource() == remove) {
- if (listModel.getSize() > 0) {
- listModel.remove(listModel.getSize() - 1);
- }
- }
- }
- }
经过这次我体会到了文档的重要性,我用的是JDK 5.0 Documentation,的确是好东西,不光可以查看类有哪些方法,如
何使用,其实里面还有n多经典的demo,也许让你挠破头皮的问题,在里面就有很详细的解释。建议使用英文版的,因为
很多中文翻译的不全或者有歧义,容易让思路陷入歧途。所有的源码我都上传到优快云,下载地址:
http://download.youkuaiyun.com/source/1851718
运行效果如图。有兴趣的可以下来看看。