How to close the window in AWT?

前端 未结 3 2045
执念已碎
执念已碎 2021-01-04 14:02

I am creating a small application using AWT. When I try to close the window, the \"close\" button doesn\'t work.

Here\'s my code:

import java.awt.*;
         


        
相关标签:
3条回答
  • 2021-01-04 14:14

    It's better to use the method public void dispose()

    Why should you have to dispose() a java.awt.Window that goes out of scope?

    f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                dispose();
             }
         }
    );
    
    0 讨论(0)
  • 2021-01-04 14:19

    Try doing it like this:

    class ExampleClass implements ActionListener, WindowListener
    {
    
    ...
    
    f.addWindowListener(this);
    
    ...
    
    public void windowDeactivated(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowOpened(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}
    
    public void windowClosing(WindowEvent e) 
    {
        System.exit(0);
    }
    
    }
    
    0 讨论(0)
  • 2021-01-04 14:21

    You could do it like this:

    f.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent we){
        System.exit(0);
      }
    });
    
    0 讨论(0)
提交回复
热议问题