import java.awt.*;
import java.awt.event.*;
import java.io.*;
class Sy
{
public static void main(String []args)
{
MyGuiFrame mgf = new MyGuiFrame("文件目录");
}
}
class MyGuiFrame extends Frame
{
private TextField tf;
private TextArea ta;
private Button btn;
private Label lab;
public MyGuiFrame(String title)
{
super(title);
setBounds(200,150,500,600);
setLayout(new FlowLayout());
tf = new TextField(50);
btn = new Button("转到");
ta = new TextArea(30,65);
add(tf);
add(btn);
add(ta);
setVisible(true);
myEvent();
}
private void myEvent()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
setVisible(false);
dispose();
System.exit(0);
}
});
tf.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ta.setText("");
getFile();
}
});
}
private void getFile()
{
String str = tf.getText();
File file = new File(str);
if(file.isDirectory())
{
String [] names = file.list();
for(String name :names)
{
ta.append(name+"\r\n");
}
}
else
{
MyDialog dia = new MyDialog(this,"系统提示",true);
dia.setStr(str);
dia.run();
}
}
}
class MyDialog extends Dialog
{
private Label lab ;
private Button btn;
private String str;
public MyDialog(Frame f, String s,boolean m)
{
super(f,s,m);
setBounds(250,200,280,200);
lab = new Label();
btn = new Button("确定");
setLayout(new FlowLayout());
add(lab);
add(btn);
this.addWindowListener(new WindowAdapter(){ //在此处加上关闭事件就行了.
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
}
public void setStr(String str)
{
this.str = str;
}
public void run()
{
String info = "对不起!你指定的目录:"+str+"是出错误的!";
lab.setText(info);
setVisible(true);
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setVisible(false);
dispose();
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
setVisible(false);
dispose();
}
});
}
}
楼主你在第85行加入
this.addWindowListener(new WindowAdapter(){ //在此处加上关闭事件就行了.
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
就可以了,你上面的代码没有给dialog加上关闭监听事件. |