3

Got a Frame in Java:

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

The frame's top right "x" close button is not working by default. How can I set that ?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222
  • 2
    You really should be using Swing components, not AWT components. That being said, replace `Frame` with `JFrame`. – user1329572 May 09 '12 at 18:37

1 Answers1

7

You should use a JFrame and then

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

But if you insist on Frame then add a listener:

AFrame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
});
Marcos
  • 4,643
  • 7
  • 33
  • 60
  • 1
    Agreed, the OP should probably be using a `JFrame`, just for reference, here's how to close an AWT frame: http://www.exampledepot.com/egs/java.awt/frame_CloseHide.html – Bart Kiers May 09 '12 at 18:41
  • 3
    `System.exit(0)` may be a bit overkill: perhaps `AFrame.this.dispose()` is a better option. See: http://stackoverflow.com/questions/258099/how-to-close-a-java-swing-application-from-the-code – Bart Kiers May 09 '12 at 18:44
  • @BartKiers thanks, I didn't know it. You learn a new thing everyday. – Marcos May 09 '12 at 21:09
  • `rame.setDefaultCloseOperation(JFrame.DISOPSE_ON_CLOSE);` – Andrew Thompson May 10 '12 at 06:56