Java 为什么不关闭窗口会出现问题

Java 为什么不关闭窗口会出现问题,java,Java,我知道这个程序需要窗口关闭功能来关闭框架,但我不知道为什么 解释会有帮助的 public class NoLayoutDemo { public static void main(String[] args) { Frame frame= new Frame("no layout teset"); frame.setLayout(null); frame.addWindowListener(new WindowAdapter() { @Override p

我知道这个程序需要窗口关闭功能来关闭框架,但我不知道为什么 解释会有帮助的

public class NoLayoutDemo {
   public static void main(String[] args) {
   Frame frame= new Frame("no layout teset");
   frame.setLayout(null);
   frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent evt) {
        System.exit(0);
     }
  }
也许是这样

   public class NoLayoutDemo {
        public static void main(String[] args) {
            Frame frame = new Frame("no layout teset");
            frame.setLayout(null);
            frame.setSize(200, 300);
            frame.show();
            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent evt) {
                    frame.dispose();
                }
            });
        }
    }

根据java文档

WINDOW_CLOSING:
If the program doesn't explicitly hide or dispose the window while processing this 
event, the window close operation is canceled.
然后你必须自己做关闭操作,否则关闭操作没有效果

希望这有帮助