import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.io.File;
import java.io.FilenameFilter;
class FileWindow extends JFrame implements ActionListener{
JTextField diretext,typetext;
JLabel direlabel,typelabel;
JButton yes;
Box boxv,boxh1,boxh2;
public String dir,type;
File file;
public String[] filename;
FileWindow(String s){
setTitle(s);
diretext = new JTextField(10);
diretext.addActionListener(this);
typetext = new JTextField(10);
typetext.addActionListener(this);
direlabel =new JLabel("请输入文件夹目录:");
typelabel =new JLabel("请输入文件类型: ");
JButton yes=new JButton("YES");
yes.addActionListener(this);
boxv=Box.createVerticalBox();
boxh1=Box.createHorizontalBox();
boxh2=Box.createHorizontalBox();
setLayout(new FlowLayout());
boxh1.add(direlabel);
boxh1.add(Box.createHorizontalStrut(8));
boxh1.add(diretext);
boxh2.add(typelabel);
boxh2.add(Box.createHorizontalStrut(17));
boxh2.add(typetext);
boxv.add(boxh1);
boxv.add(Box.createVerticalStrut(4));
boxv.add(boxh2);
boxv.add(Box.createVerticalStrut(4));
boxv.add(yes);
add(boxv);
setVisible(true);
setResizable(false);
setBounds(10,10,300,150);
validate();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==yes){
if(diretext.getText().equals("")==false&&typetext.getText().equals("")==false){
dir=diretext.getText().trim();
file=new File(dir);
type=typetext.getText().trim();
System.out.println(dir+":"+"*."+type);
filename=file.list(new FileAccept(type));
if(filename.length==0){
System.out.println("查无此文件");
}
else{
for(int i=0;i<filename.length;i++){
System.out.println(filename[i]);
}
}
}
}
else{
System.out.println("请重新输入");
}
}
}
class FileAccept implements FilenameFilter{
String str;
FileAccept(String s){
str="."+s;
}
public boolean accept(File dir,String name){
return name.endsWith(str);
}
}
public class FileWin {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileWindow filewindow=new FileWindow ("测试程序");
//String s1="D:\\";
//File file=new File();
//String[] filename=filewindow.file.list(new FileAccept(filewindow.type) );