How to close the frame by clicking on the "x" in the upper right corner?

Got a frame in Java:

 Frame AFrame = new Frame("Frame with components"); 

The top right close button "x" does not work by default. How can i install this?

+3
source share
1 answer

You have to use jframe and then

JFrame AFrame = new JFrame("Frame with components");
AFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

But if you insist on Frame, add a listener:

AFrame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
});
+7
source

All Articles