笑声演员 2021-09-03 19:05 采纳率: 33.3%
浏览 74
已结题

java jtree实现QQ好友列表,读取到了数据库图片显示不出来

public FriendJTreePanel() {
    init();
}
private void init() {
    //设置面板基本属性
    this.setBackground(Color.WHITE);
    //设置根节点
    FriendNode root = new FriendNode("root",true);
    //设置子节点,也就是分组名
    FriendNode newfriend = new FriendNode("新朋友",true);
    FriendNode myequipment = new FriendNode("我的设备0/0",true);
    FriendNode me = new FriendNode("我0/0",true);
    FriendNode friends = new FriendNode("朋友0/0",true);
    FriendNode family = new FriendNode("家人0/0",true);
    FriendNode classmate = new FriendNode("同学",true);
    //分组下添加用户子节点
    List<Userinfo> list = userinfoService.selectAllUserinfo();
    for (Userinfo userinfo : list) {
        if(!MyQQ.username.equals(userinfo.getUsername())){
            ImageIcon headimage = new ImageIcon(userinfo.getHeadimage());
            String nickname = userinfo.getNickname();
            String signature = userinfo.getSignature();
            FriendNode node = new FriendNode(headimage,nickname,1,signature);
            newfriend.add(node);
        }
    }
    //把子节点加入到根节点
    root.add(newfriend);
    root.add(myequipment);
    root.add(me);
    root.add(friends);
    root.add(family);
    root.add(classmate);

}

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, 
        boolean expanded,boolean leaf, int row, boolean hasFocus) 
{
    super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
    //将value转化为节点对象
    FriendNode  friendNode = (FriendNode)value;
    //从节点中读取图片并且将图片自适应大小、居中
    ImageIcon icon = new ImageIcon(friendNode.getHeadimage()+"");
    icon.setImage(icon.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT));
    //从节点中读取昵称和是否在线
    String isOnline="离线";
    String text="";
    
    //因为在Label中文本文字不能够通过调用相应的方法进行换行,
    //所以通过使用html的语法对文字进行换行
    if(friendNode.isFathertree()){
        text = friendNode.getNickname();
    }else if(friendNode.getIsOnline()==0){
        isOnline="[离线]";
        text="<html>" + friendNode.getNickname() + "<br/>" + friendNode.getSignature() + isOnline + " <html/>";
    } 
    else if(friendNode.getIsOnline()==1){
        isOnline="[在线]";
        text="<html>" + friendNode.getNickname() + "<br/>" + friendNode.getSignature() + isOnline + " <html/>";
    }
    //设置节点打开的图片
    this.setOpenIcon(ImageUtil.getImageIcon("img/xiala.png", 18,18));
    //设置节点关闭的图片
    this.setClosedIcon(ImageUtil.getImageIcon("img/xialaright.png", 18,18));
    //设置节点的图片
    this.setLeafIcon(ImageUtil.getImageIcon("img/xialaright.png", 18,18));
    //设置图片
    this.setIcon(icon);
    //设置文本
    this.setText(text);
    //设置图片和文本之间的距离
    this.setIconTextGap(15);
    return this;    
}

}

private ImageIcon headimage;//节点存放图片
private String nickname;//第一行文字(显示名字)
private String signature;//第二行文字(显示签名)
private int isOnline;//是否在线
private boolean fathertree;//是否为父节点

public FriendNode () {

}
/**
 * 只包含好友昵称和是否为父节点的节点构造函数
 * @param nickname
 */
public FriendNode (String nickname,boolean fathertree){
    super();
    this.nickname=nickname;
    this.fathertree=fathertree;
}
/**
 * 只包含好友昵称和是否在线的节点构造函数
 * @param nickname
 * @param isOnline
 */
public FriendNode (String nickname,int isOnline){
    super();
    this.nickname=nickname;
    this.isOnline=isOnline;
}
/**
 * 包含好友头像、昵称、是否在线的节点构造函数
 * @param headimage
 * @param nickname
 * @param isOnline
 */
public FriendNode(ImageIcon headimage,String nickname,int isOnline,String signature){
    super();
    this.headimage=headimage;
    this.nickname=nickname;
    this.isOnline=isOnline;
    this.signature=signature;
}
  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-09-03 19:21
    关注

    headimage看下这个参数是否为null.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月11日
  • 已采纳回答 9月3日
  • 创建了问题 9月3日

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程