第五次作业

package liebiao;

import java.io.File;

import javax.swing.JFrame;
import javax.swing.JList;
public class liebiao extends JFrame{

private static final File dir = null;
private static Object String;
/**

  • @param args
  • @throws IllegalAccessException
    */
    public static String[] listDirectory(File dir) throws IllegalAccessException{
    //判断File对象dir是否存在,使用File类的exists()方法
    if(!dir.exists()){
    //如果不存在,抛出异常
    throw new IllegalAccessException("目录" + dir +"不存在。");
    }
    //判断dir是否是目录,使用File类的isDirectory()方法
    if(!dir.isDirectory()){
    throw new IllegalArgumentException(dir + "不是目录");
    }

//获得指定目录dir下的文件及子目录并输出(1.list();2.listFiles())

//使用list()方法实现
String[] fileNames = dir.list();
for(String s : fileNames)
System.out.println(s);

//使用listFiles()方法实现只列出目录下的所有文件名称
File[] files = dir.listFiles();
return fileNames;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frm = new JFrame();
frm.setBounds(500,200,300,500);

frm.setVisible(true);

try {
liebiao.listDirectory(new File("F:"));
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

package liebiao;

import javax.swing.;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Scrollbar;
import java.awt.event.
;
import java.io.File;
import java.io.File;
import javax.swing.JList;

public class FileUtilsFrame {

private static JLabel infoLabel;
private static JSlider slider;
private static JList list;
private static ListModel fileName;

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub

final JFrame frm = new JFrame("");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JList jList = new JList<>(liebiao.listDirectory(new File("F:")));

frm.add(jList);
JLabel northLabel = new JLabel("files list");
frm.add(northLabel,BorderLayout.NORTH);

frm.setBounds(400, 200, 200, 500);
frm.setVisible(true);

}
}

posted @ 2016-04-17 20:24  皮革厂厂长  阅读(378)  评论(0编辑  收藏  举报