Create Frame Window With Window Close Event Example
- /*
- 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




