写这个原因是有时,用的Jar包太多,想统计下lib文件夹下所有的文件
用法:在转到前的框中输入要找文件名的目录,之后点转到就能找到里面包含的所有文件:如图
代码:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyWindowDemo
{
private Frame f;
private TextField tf;
private Button but;
private Button outBut;
private TextArea ta;
private Dialog d;//为了方便阅读程序.
private Label lab;
private Button okBut;
private FileDialog fd;
MyWindowDemo()
{
init();
}
public void init()
{
f = new Frame("my window");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(60);
but = new Button("转到");
outBut = new Button("导出");
ta = new TextArea(25,73);
d = new Dialog(f,"提示信息-self",true);
d.setBounds(400,200,240,150);
d.setLayout(new FlowLayout());
lab = new Label();
okBut = new Button("确定");
fd = new FileDialog(f, "导出");
d.add(lab);
d.add(okBut);
f.add(tf);
f.add(but);
f.add(outBut);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
outBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fd.setVisible(true);
}
});
okBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
});
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showDir();
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDir();
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void outDir()
{
}
public void showDir()
{
String dirPath = tf.getText();
File dir = new File(dirPath);
if(dir.exists() && dir.isDirectory())
{
ta.setText("");
String[] names = dir.list();
for(String name : names)
{
ta.append(name+"\r\n");
}
}
else
{
String info = "您输入的信息:"+dirPath+"是错误的,请重输";
lab.setText(info);
d.setVisible(true);
}
}
public static void main(String[] args)
{
new MyWindowDemo();
}
}