日期:2014-05-20  浏览次数:20745 次

在线等待解决 用Jtree 打印d:\\所有文件和文件名 用树形结构排列 已经写好了 为什么输出结果是空白?
package exercise;

import java.io.File;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class Jtree extends JFrame
{
private JScrollPane jsp; 
private JTree jtr;
private static DefaultMutableTreeNode root;

public Jtree()
{
init();
setBounds(50, 50, 500, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
File file=new File("d:\\homework");
root=new DefaultMutableTreeNode(file);
search(file, root);

}

public void init()
{
jtr=new JTree(root);

jsp=new JScrollPane(jtr);
  add(jsp);
   
}

public static void search(File file,DefaultMutableTreeNode node)
{
File[] f=file.listFiles();
for(int i=0;i<f.length;i++)
{
DefaultMutableTreeNode nodes=new DefaultMutableTreeNode(f[i].getName());
node.add(nodes);
if(f[i].isDirectory())
{
search(f[i],nodes);
}

}
}

public static void main(String[] args)
{

new Jtree().setVisible(true);


}

}

------解决方案--------------------
Java code

package exercise;

import java.io.File;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class Jtree extends JFrame
{
    private JScrollPane jsp; 
    private JTree jtr;
    private static DefaultMutableTreeNode root;
    
    public Jtree()
    {
        setBounds(50, 50, 500, 800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        File file=new File("d:\\1");
        root=new DefaultMutableTreeNode(file);
        search(file, root);
        init();    //最后创建JTree
        
    }
    
    public void init()
    {
        jtr=new JTree(root);
        
        jsp=new JScrollPane(jtr);
          add(jsp);
           
    }
    
    public static void search(File file,DefaultMutableTreeNode node)
    {
        File[] f=file.listFiles();
        for(int i=0;i<f.length;i++)
        {
            DefaultMutableTreeNode nodes=new DefaultMutableTreeNode(f[i].getName());
            node.add(nodes);
            if(f[i].isDirectory())
            {
                search(f[i],nodes);
            }
        
        }
    }
    
    public static void main(String[] args)
    {
    
        new Jtree().setVisible(true);
    
    
    }

}

------解决方案--------------------
探讨

嗯 谢谢了 我是试过了 的确是 能告诉我为什么吗?自己调试 很不好调试出来啊 您是用的debug模式调试出来的么?

------解决方案--------------------
init();中
jtr=new JTree(root);需要你root,开始root = null,看不到树
要等到root先赋值,在运行 jtr = new JTree(root);

------解决方案--------------------
Java code

import java.io.File;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class Jtree extends JFrame {
    private JScrollPane jsp;
    private JTree jtr;
    private static DefaultMutableTreeNode root;

    public Jtree() {
        
        setBounds(50, 50, 500, 800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        File file = new File("D:\\");
        File[] fs = file.listFiles();
        System.out.println(fs.length);
        
        root = new DefaultMutableTreeNode(file);
        init();
        
        search(file, root);
        
        this.setVisible(true);
    }

    public void init() {
        jtr = new JTree(root);
        jsp = new JScrollPane(jtr);
        add(jtr);
    }

    public static void search(File file, DefaultMutableTreeNode node) {
        File[] f = file.listFiles();
        for (int i = 0; i < f.length; i++){
            DefaultMutableTreeNode nodes = new DefaultMutableTreeNode(
                    f[i].getName());
            node.add(nodes);
            if (f[i].isDirectory()) {
                search(f[i], nodes);
            }
            else{//just here。加上这个没问题。。
                return;
            }
        }
    }
    public static void main(String args[]){
        new Jtree();
    }
}