接触java不久,再看swing编程的东西,请教各位大大指点。
8 个解决方案
#1
Java的File类是可以有一个方法判断是不是目录的
#2
我也想知道!!顶上去
#3
我能够取出文件和文件夹的名称和绝对路径,但是不知道怎么把他们生成树~~
#4
顶一下
#5
"遍历...", "树..."等词语 总让人想起递归
public static void traverse(File dir){
filltoTreeWith(dir);
if(dir.isDirectory()){
String[] children=dir.list();
for(int i=0;i<children.length;i++){
traverse(new File(dir,children[i]));
}
}
}
public static void filltoTreeWith(File dir){
//这里你把dir 放到swing的tree上。
}
public static void traverse(File dir){
filltoTreeWith(dir);
if(dir.isDirectory()){
String[] children=dir.list();
for(int i=0;i<children.length;i++){
traverse(new File(dir,children[i]));
}
}
}
public static void filltoTreeWith(File dir){
//这里你把dir 放到swing的tree上。
}
#6
试试~
#7
放到swing的tree上是什么意思,刚接触java菜菜一个 望大大解释下
#8
终于搞定了 ,谢谢~我还是帖下代码方便后人。
private JTree getJTree(File file) {
if (jTree == null) {
DefaultMutableTreeNode top = new DefaultMutableTreeNode(file
.getName());
creatNodes(top, file);
jTree = new JTree(top);
}
return jTree;
}
private void creatNodes(DefaultMutableTreeNode top, File file) {
DefaultMutableTreeNode children = null;
if(file.isDirectory()){
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
children = new DefaultMutableTreeNode(files[i].getName());
top.add(children);
creatNodes(children,files[i]);
}
}
}
private JTree getJTree(File file) {
if (jTree == null) {
DefaultMutableTreeNode top = new DefaultMutableTreeNode(file
.getName());
creatNodes(top, file);
jTree = new JTree(top);
}
return jTree;
}
private void creatNodes(DefaultMutableTreeNode top, File file) {
DefaultMutableTreeNode children = null;
if(file.isDirectory()){
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
children = new DefaultMutableTreeNode(files[i].getName());
top.add(children);
creatNodes(children,files[i]);
}
}
}
#1
Java的File类是可以有一个方法判断是不是目录的
#2
我也想知道!!顶上去
#3
我能够取出文件和文件夹的名称和绝对路径,但是不知道怎么把他们生成树~~
#4
顶一下
#5
"遍历...", "树..."等词语 总让人想起递归
public static void traverse(File dir){
filltoTreeWith(dir);
if(dir.isDirectory()){
String[] children=dir.list();
for(int i=0;i<children.length;i++){
traverse(new File(dir,children[i]));
}
}
}
public static void filltoTreeWith(File dir){
//这里你把dir 放到swing的tree上。
}
public static void traverse(File dir){
filltoTreeWith(dir);
if(dir.isDirectory()){
String[] children=dir.list();
for(int i=0;i<children.length;i++){
traverse(new File(dir,children[i]));
}
}
}
public static void filltoTreeWith(File dir){
//这里你把dir 放到swing的tree上。
}
#6
试试~
#7
放到swing的tree上是什么意思,刚接触java菜菜一个 望大大解释下
#8
终于搞定了 ,谢谢~我还是帖下代码方便后人。
private JTree getJTree(File file) {
if (jTree == null) {
DefaultMutableTreeNode top = new DefaultMutableTreeNode(file
.getName());
creatNodes(top, file);
jTree = new JTree(top);
}
return jTree;
}
private void creatNodes(DefaultMutableTreeNode top, File file) {
DefaultMutableTreeNode children = null;
if(file.isDirectory()){
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
children = new DefaultMutableTreeNode(files[i].getName());
top.add(children);
creatNodes(children,files[i]);
}
}
}
private JTree getJTree(File file) {
if (jTree == null) {
DefaultMutableTreeNode top = new DefaultMutableTreeNode(file
.getName());
creatNodes(top, file);
jTree = new JTree(top);
}
return jTree;
}
private void creatNodes(DefaultMutableTreeNode top, File file) {
DefaultMutableTreeNode children = null;
if(file.isDirectory()){
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
children = new DefaultMutableTreeNode(files[i].getName());
top.add(children);
creatNodes(children,files[i]);
}
}
}