import javax.swing.JFrame; import java.awt.Label; public class CloseWindowWithJFrame { public static void main(String[] args) { // Create a JFrame JFrame frame = new JFrame("Closing a JFrame Window"); // Create a label and add it to the frame Label label = new Label("This is a JFrame...", Label.CENTER); frame.add(label); // Set the size (in pixels) and location (center screen) of the frame // and make the frame visible frame.setSize(400, 400); frame.setLocationRelativeTo(null); frame.setVisible(true); // With a JFrame (as opposed to a Frame), this single line can be // used to cause the window to close frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }