日期:2014-05-20  浏览次数:20614 次

GUI的好奇怪现象,找不到原因!!
[code=Java][/code]
package com.frametest;
import java.awt.*;
public class FrameTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f = new Frame("www.Jcson");
f.add(new Button("confirm")); //曾加按钮.
f.setSize(1000, 900);
f.setVisible(true); //设置可见.
try {
Thread.sleep(2000); //线程休息2秒.
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
//f.setVisible(false);
f.addWindowListener(new YourWindowListener()); //添加YourWindowListener实例.

}

}



import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class YourWindowListener extends WindowAdapter { //继承WindowAdapter类.
public void windowClosing(WindowEvent e){
e.getWindow().setVisible(false); //设置不可见.
e.getWindow().dispose(); //关闭窗口.
System.exit(0);
}
}

请问,为什么运行时的窗口有时点击一次可以关闭,有时要点击两三次才能关闭,好奇怪!!

------解决方案--------------------
其实如果用 JFrame 的话,直接可以:
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
更简洁些。