本帖最后由 吴亨 于 2012-1-14 21:05 编辑
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class TextGui
{
private Frame f;
private Button btn;
private TextField tf;
private TextArea ta;
public static void main(String[] args) throws Exception
{
new TextGui();
}
public TextGui() throws Exception
{
init() ;
}
public void init() throws Exception
{
f = new Frame("文件搜索");
f.setBounds(200,100,600,600);
f.setLayout(new FlowLayout());
f.setVisible(true);
btn = new Button("显示文件");
tf = new TextField(60);
ta = new TextArea(30,70);
f.add(btn);
f.add(tf);
f.add(ta);
myEvent();
}
public void myEvent() throws Exception
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
f.dispose();
System.exit(0);
}
});
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDir();
}
});
}
public void showDir()
{
File dir = new File( tf.getText());
if(dir.exists() && dir.isDirectory())
{
String[] names = dir.list();
ta.setText("");
for(String name : names)
{
ta.append(name+"\r\n");
}
}
else
{
new MyDialog().getMyDialog();
}
}
class MyDialog
{
private Dialog dg ;
public MyDialog()
{
dg = new Dialog(f,"错误提示",true);
dg.setBounds(300,200,300,300);
dg.setVisible(true);
dg.addWindowListener(new WindowAdapter() //这句话不能执行吗?
{
public void windowClosing(WindowEvent e)
{
dg.dispose();
System.exit(0);
}
});
}
public Dialog getMyDialog()
{
return dg;
}
}
}
为什么我的对话框关不了? |