Mega Code Archive

 
Categories / Java Tutorial / Swing
 

JFrame with Label and Window Listener to Handle Closing the Frame

import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.WindowConstants; class WindowEventHandler extends WindowAdapter {   public void windowClosing(WindowEvent evt) {     System.exit(0);   } } public class TJFrame {   public static void main(String[] args) {     JFrame frame = new JFrame("Swing Frame");     JLabel label = new JLabel("This is a Swing frame", JLabel.CENTER);     frame.add(label);     frame.addWindowListener(new WindowEventHandler());     frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);     frame.setSize(350, 200); // width=350, height=200     frame.setVisible(true); // Display the frame   } }