Search code examples
javauser-interfacewindowwindowlistener

How to close a frame in Java without closing the cmd prompt?


Here is the frame (i.e. java.awt.Frame) code. When I click the close button on the window it doesn't close and every time I have to close the the cmd prompt from where I launch this program. How to make it close?

import java.awt.*;

public class FrameExample {

private Frame f;

public FrameExample () {
    f=new Frame("Hi its Harish");
}

public void launchFrame() {
    f.setSize(470,470);
    f.setVisible(true);
}

public static void main(String args[]) {
    FrameExample guiWindow=new FrameExample();
    guiWindow.launchFrame();
}
}

Solution

  • add this listener into your code

    f.addWindowListener(new WindowAdapter(){
       public void windowClosing(WindowEvent we){
          System.exit(0);
       }
    });