help中的例子?是什么,贴出来看看。

解决方案 »

  1.   

      // Display an icon and a string for each object in the list. 
     class MyCellRenderer extends JLabel implements ListCellRenderer {
         final static ImageIcon longIcon = new ImageIcon("long.gif");
         final static ImageIcon shortIcon = new ImageIcon("short.gif");     // This is the only method defined by ListCellRenderer.
         // We just reconfigure the JLabel each time we're called.     public Component getListCellRendererComponent(
           JList list,
           Object value,            // value to display
           int index,               // cell index
           boolean isSelected,      // is the cell selected
           boolean cellHasFocus)    // the list and the cell have the focus
         {
             String s = value.toString();
             setText(s);
             setIcon((s.length() > 10) ? longIcon : shortIcon);
           if (isSelected) {
                 setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
             else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
        setEnabled(list.isEnabled());
        setFont(list.getFont());
             return this;
         }
     } String[] data = {"one", "two", "three", "four"};
     JList dataList = new JList(data);
     dataList.setCellRenderer(new MyCellRenderer());
    另外:ImageIcon一定要是gif么?
      

  2.   

    试试下面的代码:
    class MyCellRenderer extends JLabel implements ListCellRenderer{Icon [] icons;
    public MyCellRenderer(Icon[] icons)
    {
      this.icons=icons;
      setOpaque(true);
    }public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus)
    {
      if(value!=null)
      {
        String text=value.toString();
        setText(text);
      }
      if(index!=-1 && index<icons.length)
        setIcon(icons[index]);
      if(isSelected)
      {
        setBackground(list.getSelectionBackground());
        setForeground(list.getSelectionForeground());
      }
      else
      {
        setBackground(list.getBackground());
        setForeground(list.getForeground());
      }
      return this;
    }
    }
    .....
    String[] picname={"long","short"};
    Icon icon0=new ImageIcon("long.gif");
    Icon icon1=new ImageIcon("short.gif");
    Icon[] icons={icon0,icon1};
    JList dataList=new JList(picname);
    dataList.setRenderer(new MyCellRenderer(icons));
    .....