2012-05-09 38 views

回答

7

您应该使用JFrame然后

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

但是如果你坚持帧,然后添加一个侦听器:

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

同意,OP应该使用'JFrame',仅供参考,以下是如何关闭AWT框架:http://www.exampledepot.com/egs/java.awt/frame_CloseHide.html –

+3

'System.exit (0)'可能有点矫枉过正:也许'AFrame.this.dispose()'是一个更好的选择。请参阅:http://stackoverflow.com/questions/258099/how-to-close-a-java-swing-application-from-the-code –

+0

@BartKiers谢谢,我不知道它。你每天都会学到一件新事物。 –

相关问题