📄 friendlistview.java
字号:
/* * FriendListView.java * * Created on 2005年12月20日, 下午6:38 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */package javaicqproj;/** * * @author hurysa */import javax.swing.*;import javax.swing.border.*;import java.awt.*;import java.util.*;public class FriendListView extends JList implements Observer{ NameAndPicListModel model=new NameAndPicListModel(); ListCellRenderer renderer=new NameAndPicListCellRenderer(); private String [] picsonline=new String []{"1.jpg","3.jpg","5.jpg","7.jpg"}; private String [] picsoffline=new String[]{"2.jpg","4.jpg","6.jpg","8.jpg"}; /** * Creates a new instance of FriendListView */ public FriendListView() { this.setModel(model); this.setCellRenderer(renderer); } /** Implements method update of interface java.util.Observer */ public void update(Observable o, Object arg) { FriendInfoModel m=(FriendInfoModel)o; int picid; model.removeAllElements(); for(int p=0;p<m.getFriendNum();p++){ picid=Integer.parseInt(m.getFriendPic().get(p).toString()); if(m.getFriendStatus().get(p).equals("上线")){ model.addElement(new Object[]{m.getFriendNames().get(p),new ImageIcon(picsonline[picid])});} else model.addElement(new Object[]{m.getFriendNames().get(p),new ImageIcon(picsoffline[picid])}); }//for } class NameAndPicListModel extends DefaultListModel{ public NameAndPicListModel(){ removeAllElements(); } public NameAndPicListModel(Vector friendnames,String [] pics){ removeAllElements(); for(int i=0;i<friendnames.size();i++){ addElement(new Object[]{friendnames.get(i),new ImageIcon(pics[i])}); } } public String getName(Object object){ Object[] array=(Object[])object; return(String) array[0]; } public Icon getIcon(Object object){ Object[] array=(Object[]) object; return (Icon) array[1]; } } class NameAndPicListCellRenderer extends JLabel implements ListCellRenderer{ private Border lineBorder=BorderFactory.createLineBorder(Color.red,2), emptyBorder =BorderFactory.createEmptyBorder(2,2,2,2); public NameAndPicListCellRenderer(){ setOpaque(true); } public Component getListCellRendererComponent(JList list,Object value,int index, boolean isSelected,boolean cellHasFocus){ NameAndPicListModel model=(NameAndPicListModel) list.getModel(); setText(model.getName(value)); 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; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -