1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
/* Create Frame Window With Window Close Event Example This java example shows how to create frame window and handle windowClosing event using WindowAdapter class. */ import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /* * To create a stand alone window, class should be extended from * Frame and not from Applet class. */ public class CreateWindowWithEventsExample extends Frame{ CreateWindowWithEventsExample(String title){ //call the superclass constructor with the specified title super(title); //add window event adapter addWindowListener(new MyWindowAdapter(this)); //set window size using setSize method this.setSize(300,300); //show window using setVisible method this.setVisible(true); } //extend WindowAdapter class MyWindowAdapter extends WindowAdapter{ CreateWindowWithEventsExample myWindow = null; MyWindowAdapter(CreateWindowWithEventsExample myWindow){ this.myWindow = myWindow; } //implement windowClosing method public void windowClosing(WindowEvent e) { //hide the window when window's close button is clicked myWindow.setVisible(false); } } public static void main(String[] args) { CreateWindowWithEventsExample myWindow = new CreateWindowWithEventsExample("Window with event Example"); } } |
Example Output
Add Comment