JFrame has the setDefaultCloseOperation method to control what the JFrame's close button does.
The setDefaultCloseOperation method can take one of these static finals defined in the JFrame class:
WindowConstants.DO_NOTHING_ON_CLOSE. Don't do anything.
WindowConstants.HIDE_ON_CLOSE (the default). Hides the frame after invoking any registered WindowListener objects.
WindowConstants.DISPOSE_ON_CLOSE. Hides and disposes the frame after invoking any registered WindowListener objects.
JFrame.EXIT_ON_CLOSE. Exits the application using the System.exit method.
import javax.swing.JFrame;
import javax.swing.UIManager;
public class SimplestFrame {
public static void main(String[] args) {
JFrame aWindow = new JFrame("This is the Window Title");
aWindow.setBounds(50, 100, 300, 300);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aWindow.setVisible(true);
}
}