This Java Swing tip illustrates a method of using WindowListener for closing JFrames. This code snippet may be used in any application where it is required to exit from the application when the frame is closed.

 import javax.swing.JFrame;
import java.awt.event.*;

public class FrameClose2 {
    public static void main(String[] args) {
        JFrame mainFrame = new JFrame();
        // Exit app when frame is closed.
        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent ev) {
        mainFrame.setSize(320, 240);