import java.awt.*;
import java.awt.event.*;
import java.io.*;
class AwtFileList
{
private Frame f;
private Button b;
private TextField tf;
private TextArea ta;
private Label la;
private Dialog d;
private Button b1;
AwtFileList()
{
init();
}
public void init()
{
f= new Frame("列出目录下清单");
f.setLayout(new FlowLayout());
b=new Button("转到");
tf=new TextField(41);
ta=new TextArea(20,40);
f.setBounds(200,200,400,400);
f.add(tf);
f.add(b);
f.add(ta);
addEvent();
f.setVisible(true);
}
public void addEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent w)
{
System.exit(0);
}
});
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent w)
{
d.setVisible(false);
}
});
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent a)
{
d.setVisible(false);
}
});
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent a)
{
String dir = tf.getText();
//tf.setText("");
File fi = new File(dir);
if(fi.exists() && fi.isDirectory())
{
ta.setText("");//清空
String[] list = fi.list();
for(String l : list)
{
ta.append(l+"\r\n");
}
}
else
{
//f代表该对话框与那个窗体相关联
d =new Dialog(f,"错误提示",true);
b1 = new Button("确定");
String s = "请检查您输入的目录"+dir+"有误";
la = new Label(s);
d.setBounds(200,200,200,200);
d.setLayout(new FlowLayout());
d.add(la);
d.add(b1);
d.setVisible(true);
}
}
});
}
public static void main(String[] args)
{
new AwtFileList();
}
}
|
-
捕获.PNG
(8.68 KB, 下载次数: 5)
报错提示
|