import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;public class TreeSample3 extends JFrame
{
private JTree tree;

public TreeSample3()
{
TreeNode root=new DefaultMutableTreeNode("Root");
tree=new JTree(new SimpleTreeModel(root));
JScrollPane scrollPanel=new JScrollPane(tree,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
this.getContentPane().add(scrollPanel);

this.setSize(400,400);
this.setLocation(100,100);
this.setVisible(true);
}

public static void main(String args[])
{
TreeSample3 app=new TreeSample3();
}
}class SimpleTreeModel extends DefaultTreeModel
{
public SimpleTreeModel(TreeNode root)
{
super(root);//这边用这个是什么意思?
}

public Object getChild(Object parent, int index)
{
return new DefaultMutableTreeNode((String)((DefaultMutableTreeNode)parent).getUserObject()+index);
}

public int getChildCount(Object parent)
{
return 5;
}
}

解决方案 »

  1.   

    调用父类的构造函数,即调用public DefaultTreeModel(root){……}
      

  2.   

    class SimpleTreeModel extends DefaultTreeModel{ 
      public SimpleTreeModel(TreeNode root){ 
        super(root);//这边用这个是什么意思? 
      } 
      ...
    }
    执行SimpleTreeModel的父类DefaultTreeModel的构造函数DefaultTreeModel(TreeNode root)。
      

  3.   

    super关键字,用于调用父类的有关属性和方法(前提是父类的被调用方法和属性不是private类型)
      public SimpleTreeModel(TreeNode root){ 
        super(root);//这边用这个是什么意思? 
      } 调用父类DefaultTreeModel的构造函数中参数类型为TreeNode那个构造方法。。其余:
    super.方法名()   调用父类的方法super.属性名   调用父类的属性