1.java站如何利用TreeNode构造自定义的源码树结构?
java站如何利用TreeNode构造自定义的树结构?
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
class Mytree extends JFrame
{
Mytree(String s)
{
super(s);
Container con=getContentPane();
DefaultMutableTreeNode root=new DefaultMutableTreeNode("c:\\");
DefaultMutableTreeNode t1=new DefaultMutableTreeNode("备份资料");
DefaultMutableTreeNode t2=new DefaultMutableTreeNode("Java学习");
DefaultMutableTreeNode t1_1=new DefaultMutableTreeNode("思维论坛精华帖子");
DefaultMutableTreeNode t1_2=new DefaultMutableTreeNode("来往邮件");
DefaultMutableTreeNode t2_1=new DefaultMutableTreeNode("视频教程");
DefaultMutableTreeNode t2_2=new DefaultMutableTreeNode("Java 3D");
JTree tree=new JTree(root);
root.add(t1);
root.add(t2);
t1.add(t1_1);
t1.add(t1_2);
t2.add(t2_1);
t2.add(t2_2);
JScrollPane scrollpane=new JScrollPane(tree);
con.add(scrollpane);
setSize(,);
setVisible(true);
validate();
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}
public class Example5_
{
public static void main(String[] args)
{
new Mytree("利用TreeNode构造树");
}
}
应用结点TreeNode构造树的步骤如下:
1定义结点
2定义树,同时确定树的源码原型继承源码根结点
3将子结点添加到根结点中
运行程序如下图: