Java JFrame handle window event via WindowListener
import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JFrame; class My implements WindowListener { public void windowClosing(WindowEvent e) { System.out.println("closing"); System.exit(0);//from w w w . jav a 2 s.c o m } public void windowOpened(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } } public class Main { public static void main(String[] args) { JFrame window = new JFrame("java2s.com"); window.setBounds(30, 30, 300, 300); window.addWindowListener(new My()); window.setVisible(true); } }