码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA第五次作业

时间:2016-04-17 00:22:39      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

import java.awt.BorderLayout;

import java.awt.Color;

import java.io.File;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

public class FileUtils {

/**

列出指定文件夹(目录)中的所有文件或目录的名称

@param dir File类型 指定的文件夹(目录)

@return

@throws IllegalAccessException

/

public static String listDirectory(File dir) throws IllegalAccessException{

//判断dir所关联的文件和目录是否存在

if(!dir.exists()){

//如果不存在,那么抛出异常

throw new IllegalAccessException("目录" + dir + "不存在。");

}

//判断dir所关联的是否是一个目录

if(!dir.isDirectory()){

throw new IllegalAccessException(dir + "不是目录");

}

/用传递进来 的File对象dir调用list()方法获得

当前目录(dir)下的所有文件和文件夹的名称。

*/

String[] files = dir.list();

String m ="";

for(String a : files){

m=m+a+"\n";}

return m;

}

/**

@param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

JFrame frame=new JFrame();

JPanel main_panel =new JPanel(new BorderLayout());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

String[] itme = {".gif",".bmp"};

JComboBox frm=new JComboBox(itme);

frm.setEnabled(true);

frm.setEditable(true);

frm.setMaximumRowCount(5);

frm.setBounds(230,30,130,25);

frame.setBounds(300,200,350,300);

frame.setVisible(true);

JTextArea main_text =new JTextArea();

main_text.setBackground(Color.white);

JScrollPane z=new JScrollPane();

z.setViewportView(main_text);

main_text.setEnabled(false);

main_panel.add(frm,BorderLayout.NORTH);

main_panel.add(z,BorderLayout.CENTER);

frame.add(main_panel);

try {

String str = FileUtils.listDirectory(new File("D:\FileUtils"));

main_text.setText(str);

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

JAVA第五次作业

标签:

原文地址:http://www.cnblogs.com/zhaofanchao/p/5399725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!