求助,jtree实现好友列表问题

初学者jtree实现好友列表,登录到主界面还正常一点节点展开图标就出现问题如图
图片说明
图片说明
图片说明
相关代码如下
主界面代码

import java.awt.*;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;

import account.MyUtil;
import account.U;
import account.User;
import business.FriTreeNode;
import business.FriendCR;
import business.MyTreeUi;

import java.awt.event.*;

public class MainView extends JFrame {

    private static final long serialVersionUID = 1L;
    JPanel jp, jp2;
    JButton jb1, jb2, jb3, jb4jb5, jb6, jb7, jb, jb8, jb9, jb10;
    JTextField jtf1, jtf2, jtf3;
    JScrollPane jsp;
    JTree jt;
    JLabel jl1, jl2, jl3, jl4, jl5;

    public MainView(User u) {
        this.setBounds(1300, 210, 372, 715);
        this.setDefaultCloseOperation(3);
        this.setLayout(null);

        Font f1 = new Font("微软雅黑", Font.PLAIN, 24);
        Font f2 = new Font("微软雅黑", Font.PLAIN, 15);

        jp = new JPanel();
        jp.setBounds(0, 0, 372, 700);
        jp.setLayout(null);
        this.getContentPane().add(jp);

        // 头像
        jl1 = MyUtil.getJLabel("image/touxiang/timg2.jpg", 14, 56, 90, 90);
        jp.add(jl1);

        // 昵称
        jl1 = new JLabel();
        jl1.setBounds(114, 52, 108, 33);
        jl1.setFont(f1);
        jl1.setText(u.getNiCheng());
        jp.add(jl1);

        // 签名
        jl2 = new JLabel();
        jl2.setBounds(114, 95, 147, 20);
        jl2.setFont(f2);
        jl2.setText(u.getQianming());
        jp.add(jl2);

        // 搜素
        jtf1 = new JTextField();
        jtf1.setBounds(1, 160, 331, 33);
        jp.add(jtf1);

        // 搜索图标
        jb1 = MyUtil.getJButton("image/logo/searchimage.png", 332, 160, 38, 32);
        jp.add(jb1);


        //背景
        JLabel imgLabel =MyUtil.getJLabel("image/skin/main.jpg",0, 0, this.getWidth(),190);
        jp.add(imgLabel);

        //测试按钮
        JButton jb=MyUtil.getOqupaJButton("image/logo/1.png", 30, 20, 30, 30);

        jp.add(jb);


        FriTreeNode root=new FriTreeNode("分组");
        FriTreeNode friendList=new FriTreeNode("我的好友");
        FriTreeNode friend=new FriTreeNode("朋友");
        FriTreeNode family=new FriTreeNode("家人");
        FriTreeNode stranger=new FriTreeNode("陌生人");
        FriTreeNode blackList=new FriTreeNode("黑名单");
        FriTreeNode others=new FriTreeNode("其它");


        for(int i=0;i<u.getFriend().size();i++) {
            if(u.getFriend().get(i).getRelation()==1) {
                friendList.add(new FriTreeNode(u.getFriend().get(i)));
            }
            else if(u.getFriend().get(i).getRelation()==2) {
                friend.add(new FriTreeNode(u.getFriend().get(i)));
            }
            else if(u.getFriend().get(i).getRelation()==3) {
                family.add(new FriTreeNode(u.getFriend().get(i)));
            }
            else if(u.getFriend().get(i).getRelation()==4) {
                stranger.add(new FriTreeNode(u.getFriend().get(i)));
            }
            else if(u.getFriend().get(i).getRelation()==5) {
                blackList.add(new FriTreeNode(u.getFriend().get(i)));
            }
            else{
                others.add(new FriTreeNode(u.getFriend().get(i)));
            }
        }
        root.add(friendList);
        root.add(friend);
        root.add(family);
        root.add(stranger);
        root.add(blackList);
        root.add(others);

        jt=new JTree(root);
        jt.setRowHeight(0);
        jt.setCellRenderer(new FriendCR());
        jt.setRootVisible(false);

        jt.setUI(new MyTreeUi());
        //DefaultTreeModel dtm=new DefaultTreeModel(root);
        //jt.setModel(dtm);
        jt.setShowsRootHandles(true);



        jsp=new JScrollPane(jt);
        jsp.setBounds(0,192,this.getWidth(),480);
        jp.add(jsp);

        this.setUndecorated(true);
        this.setVisible(true);
    } 

节点类代码

 public class FriTreeNode extends DefaultMutableTreeNode{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    /**
     * 
     */

    private int id;
    private String nicheng;
    private String headIcon;
    private String qianming;
    private String online;
    private String text;
    public FriTreeNode(User u) {
        id=u.getId();
        nicheng=u.getNiCheng();
        qianming=u.getQianming();
        online=u.getOnline();
        headIcon=u.getHeadIcon();
    }

    public FriTreeNode(String text) {
        this.text=text;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    public FriTreeNode() {

    }
    public void setId(int id) {
        this.id = id;
    }
    public void setNicheng(String nicheng) {
        this.nicheng = nicheng;
    }
    public void setHeadIcon(String headIcon) {
        this.headIcon = headIcon;
    }
    public void setQianming(String qianming) {
        this.qianming = qianming;
    }
    public void setOnline(String online) {
        this.online = online;
    }


    public int getId() {
        return id;
    }

    public String getNicheng() {
        return nicheng;
    }

    public String getHeadIcon() {
        return headIcon;
    }

    public String getQianming() {
        return qianming;
    }

    public String getOnline() {
        return online;
    }


}

treecellrenderer代码

 public class FriendCR extends JLabel implements TreeCellRenderer {

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
            boolean leaf, int row, boolean hasFocus) {

        FriTreeNode ftn = (FriTreeNode)value;
        if(ftn!=tree.getModel().getRoot()&&ftn.getLevel()==2&&ftn.getParent()!=tree.getModel().getRoot()){
            Image ima = new ImageIcon(ftn.getHeadIcon()).getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT);
            this.setIcon(new ImageIcon(ima));
            this.setText("<html>" + ftn.getNicheng() + "<br/>" + ftn.getQianming() + "<html/>");
            this.setIconTextGap(15);
        }
        if (ftn.getLevel()==0|ftn.getLevel()==1) {
            this.setText(ftn.getText());
        }
        return this;
    }

}

2个回答

渲染代码treecellrenderer不要继承JLabel,这样导致的后果是树的所有0级1级节点返回的是最后一个被渲染的控件形状。
修改FriendCR类如下(其他类不变):
public class FriendCR implements TreeCellRenderer {//extends JLabel

@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
        boolean leaf, int row, boolean hasFocus) {

    FriTreeNode ftn = (FriTreeNode)value;
    JLabel label= new JLabel();
    if(ftn!=tree.getModel().getRoot()&&ftn.getLevel()==2&&ftn.getParent()!=tree.getModel().getRoot()){
        Image ima = new ImageIcon(ftn.getHeadIcon()).getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT);
        label.setIcon(new ImageIcon(ima));
        label.setText("<html>" + ftn.getNicheng() + "<br/>" + ftn.getQianming() + "<html/>");
        label.setIconTextGap(15);
    }
    if (ftn.getLevel()==0|ftn.getLevel()==1) {
        label.setText(ftn.getText());
    }
    return label;
}

}
运行结果如下:
图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问