package JTree;

 import java.awt.Component;
import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer; public class IconNodeRenderer extends DefaultTreeCellRenderer { //继承该类
//重写该方法
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); //调用父类的该方法
Icon icon = ((IconNode) value).getIcon();//从节点读取图片
String txt=((IconNode) value).getText(); //从节点读取文本
setIcon(icon);//设置图片
setText(txt);//设置文本
return this;
}
} class IconNode extends DefaultMutableTreeNode {
protected Icon icon;
protected String txt; //只包含文本的节点构造
public IconNode(String txt)
{
super();
this.txt=txt;
} //包含文本和图片的节点构造
public IconNode(Icon icon,String txt)
{
super();
this.icon = icon;
this.txt = txt;
}
public void setIcon(Icon icon)
{
this.icon = icon;
} public Icon getIcon()
{
return icon;
} public void setText(String txt)
{
this.txt=txt;
} public String getText()
{
return txt;
}
}
package JTree;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath; public class text
{
JFrame frame; public text()
{ frame=new JFrame("树");
frame.setSize(150,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//java.net.URL imgURL=getClass().getResource("");
IconNode root1=new IconNode(new ImageIcon("images/3.jpg"),"高中同学");
IconNode root2=new IconNode(new ImageIcon("i"),"初中同学"); root1.add(new IconNode(new ImageIcon("5.png"),"雅君"));
root1.add(new IconNode(new ImageIcon("1.png"),"伟旭"));
root1.add(new IconNode(new ImageIcon("2.png"),"宜群"));
root2.add(new IconNode(new ImageIcon("2.png"),"彬强"));
root2.add(new IconNode(new ImageIcon("1.png"),"小强")); IconNode Root=new IconNode(null,null);//定义根节点
Root.add(root1);//定义二级节点
Root.add(root2);//定义二级节点 final JTree tree = new JTree(Root);//定义树
tree.setCellRenderer(new IconNodeRenderer()); //设置单元格描述
tree.setEditable(false); //设置树是否可编辑
tree.setRootVisible(false);//设置树的根节点是否可视
tree.setToggleClickCount(1);//设置单击几次展开数节点 DefaultTreeCellRenderer cellRenderer=(DefaultTreeCellRenderer)tree.getCellRenderer();//获取该树的Renderer
cellRenderer.setClosedIcon(new ImageIcon("2.gif"));//关闭打开图标
cellRenderer.setOpenIcon(new ImageIcon("2.gif"));//设置展开图标 //测试事件
tree.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount()==2)//双击节点
{
TreePath path=tree.getSelectionPath();//获取选中节点路径
IconNode node=(IconNode)path.getLastPathComponent();//通过路径将指针指向该节点
if(node.isLeaf())//如果该节点是叶子节点
{
//DefaultTreeModel model=(DefaultTreeModel)tree.getModel();//获取该树的模型
//model.removeNodeFromParent(node);//从本树删除该节点
node.setIcon(new ImageIcon("3.png"));//修改该节点的图片
node.setText("双击");//修改该节点的文本
tree.repaint();//重绘更新树
System.out.println(node.getText());
}
else//不是叶子节点
{
} }
}
}); JScrollPane sp = new JScrollPane(tree);
frame.getContentPane().add(sp, BorderLayout.CENTER);
frame.setVisible(true);
} public static void main(String[] args)
{
new text();
}
}
05-11 09:11