代码如下:
import java.io.*;
import java.awt.*;
public class FileDisplay
{
public static void main(String[] args)
{
Frame f = new Frame(" Test For FileDialog");
TextArea text = new TextArea(40,400);
f.add(text);
f.setSize(600,500);
f.setVisible(true);

FileDialog fd = new FileDialog(f,"文件对话框",FileDialog.LOAD);
fd.setVisible(true);
String path = fd.getDirectory();
String name = fd.getFile();
String si = path + name;

File file = new File(si);
try{
BufferedReader input = new BufferedReader(new FileReader(file));
String s;
s = input.readLine();
while(s!=null)
{
text.append(s + "\n");
s = input.readLine();
}
input.close();
}catch(IOException e){e.printStackTrace();}
}
}当创建出界面时,不管点击X号,还是选接系统菜单,就是关闭不了程序,只有借助任务管理器才能关闭,不知这是为什么,我遇到好几次这样的情况了。

解决方案 »

  1.   


     Frame f = new Frame(" Test For FileDialog");
            TextArea text = new TextArea(40, 400);
            f.add(text);
            //增加如下代码
            f.addWindowListener(new WindowAdapter() {            @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            f.setSize(600, 500);
            f.setVisible(true);
      

  2.   

    awt和swing有点不同利用事件监听
    然后System.out.exit(0);
    http://topic.csdn.net/u/20081001/22/a805c17b-fe62-47e7-903c-4c846fbf341c.html
    http://topic.csdn.net/u/20081001/22/ba4cb409-8bbf-4665-b2b6-0517f9c0d1db.html
      

  3.   

    要关闭Java视窗,需要单独为视窗注册一个关闭监听器才可以!内容写
    System.exit(0);
      

  4.   

    或者在前面设置JFrame属性的时候,增加1个f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);