想问一下.要弄一个显示一个本地计算机中的文件夹的树形结构是用哪个类来写的?.就是跟"我的电脑"左边的树形结构一样的.
解决方案 »
- java连接Access数据库Update语句没错误但是数据库中不显示更新内容
- 高手进进 非常感谢 TestArea 显示问题 滚动条能不能显示在最下端
- 很奇怪,不用设置CLASSPATH也可已编译Java文件
- 刚学完类这一章,感觉做这个题目好像知道思路,但写出来的东西就是不能运行,大家看下这个应该怎么着手编写思路啊
- 请问关于键盘输入回车问题。
- 麻烦大家进来看看这两个JAVA题
- 如何才能使JVM Crash
- String数据类型由一个类来定义,那么请问...
- 这有什么区别吗???
- 请教大侠:Jbuilder7下如何配置DataSource?
- 分解一个字符串到数组中……
- 关于一个Timer的问题,这东东怎么只会打印一次,服务器并没有停
import java.util.ArrayList;public class ListAll
{
//用于判断目录或文件所处的层次
private static int time;
// 递归的方法
public static void deepList(File file)
{
if (file.isFile() || 0 == file.listFiles().length)
{
return;
}
else
{
File[] files = file.listFiles();
files = sort(files);
for(File f : files)
{
StringBuffer output = new StringBuffer();
if(f.isFile())
{
output.append(getTabs(time));
output.append(f.getName());
}
else
{
output.append(getTabs(time));
output.append(f.getName());
output.append("\\");
}
System.out.println(output);
if(f.isDirectory())
{
time++;
deepList(f);
time--;
}
}
}
} // 整理文件数组,使得目录排在文件之前
private static File[] sort(File[] files)
{
ArrayList<File> sorted = new ArrayList<File>(); // 寻找到所有的目录
for (File f : files)
{
if (f.isDirectory())
{
sorted.add(f);
}
}
// 寻找到所有的文件
for (File f : files)
{
if (f.isFile())
{
sorted.add(f);
}
} return sorted.toArray(new File[files.length]);
}
//判断需要加多少 tab的方法
private static String getTabs(int time)
{
StringBuffer buffer = new StringBuffer();
for(int i = 0; i < time; i++)
{
buffer.append("\t");
}
return buffer.toString();
}
public static void main(String[] args)
{
File file = new File("D:\\ZZZ");
deepList(file);
}
}
控制台实现的。
import javax.swing.tree.*;/**
* This program shows a simple tree.
* @version 1.02 2007-08-01
* @author Cay Horstmann
*/
public class SimpleTree
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new SimpleTreeFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}/**
* This frame contains a simple tree that displays a manually constructed tree model.
*/
class SimpleTreeFrame extends JFrame
{
public SimpleTreeFrame()
{
setTitle("SimpleTree");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // set up tree model data DefaultMutableTreeNode root = new DefaultMutableTreeNode("World");
DefaultMutableTreeNode country = new DefaultMutableTreeNode("USA");
root.add(country);
DefaultMutableTreeNode state = new DefaultMutableTreeNode("California");
country.add(state);
DefaultMutableTreeNode city = new DefaultMutableTreeNode("San Jose");
state.add(city);
city = new DefaultMutableTreeNode("Cupertino");
state.add(city);
state = new DefaultMutableTreeNode("Michigan");
country.add(state);
city = new DefaultMutableTreeNode("Ann Arbor");
state.add(city);
country = new DefaultMutableTreeNode("Germany");
root.add(country);
state = new DefaultMutableTreeNode("Schleswig-Holstein");
country.add(state);
city = new DefaultMutableTreeNode("Kiel");
state.add(city); // construct tree and put it in a scroll pane JTree tree = new JTree(root);
add(new JScrollPane(tree));
} private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
}
就是要这种效果的.点击的时候再读取的.用JTree就可以了吗?