Handle a window closing event
import java.awt.Button; import java.awt.Dimension; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; public class Main extends JFrame { public static void main(String[] args) { Main frame = new Main(); frame.setSize(new Dimension(200, 200)); frame.add(new Button("Hello World")); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.setVisible(true); } }