Source of CloseWindowDemo.java


  1: import javax.swing.JButton;
  2: import javax.swing.JFrame;
  3: import javax.swing.JLabel;
  4: import javax.swing.JPanel;
  5: import javax.swing.WindowConstants;
  6: import java.awt.BorderLayout;
  7: import java.awt.Color;
  8: import java.awt.Container;
  9: import java.awt.FlowLayout;
 10: import java.awt.event.ActionEvent;
 11: import java.awt.event.ActionListener;
 12: import java.awt.event.WindowAdapter;
 13: import java.awt.event.WindowEvent;
 14: 
 15: /**
 16:  Demonstration of programming the close-window button.
 17: */
 18: public class CloseWindowDemo extends JFrame
 19: {
 20:     public static final int WIDTH = 300;
 21:     public static final int HEIGHT = 200;
 22: 
 23:     public CloseWindowDemo( )
 24:     {
 25:         setSize(WIDTH, HEIGHT);
 26:         setDefaultCloseOperation(
 27:                 WindowConstants.DO_NOTHING_ON_CLOSE);
 28:         addWindowListener(new InnerDestroyer( ));
 29:         setTitle("Close Window Demo"); 
 30:         Container contentPane = getContentPane( );
 31:         contentPane.setLayout(new BorderLayout( ));
 32: 
 33:         JLabel message = new JLabel(
 34:                              "Please don't click that button.");
 35:         contentPane.add(message, BorderLayout.CENTER);
 36:     }
 37: 
 38:     //An inner class that is the window listener.
 39:     private class InnerDestroyer extends WindowAdapter
 40:     {
 41:         //Displays a window that checks if the user wants to exit.
 42:         public void windowClosing(WindowEvent e) 
 43:         {
 44:             ConfirmWindow askWindow = new ConfirmWindow( );
 45:             askWindow.setVisible(true);
 46:         }
 47:     }
 48:     
 49:     //An inner class to be used with the inner class 
 50:     //InnerDestroyer. Checks if the user wants to exit.
 51:         private class ConfirmWindow extends JFrame
 52:                                 implements ActionListener
 53:     {
 54:         public static final int WIDTH = 200;
 55:         public static final int HEIGHT = 100;
 56: 
 57:         public ConfirmWindow( )
 58:         {
 59:             setSize(WIDTH, HEIGHT);
 60:             Container confirmContent = getContentPane( );
 61:             confirmContent.setBackground(Color.WHITE);
 62:             confirmContent.setLayout(new BorderLayout( ));
 63: 
 64:             JLabel msgLabel = new JLabel(
 65:                            "Are you sure you want to exit?");
 66:             confirmContent.add(msgLabel, BorderLayout.CENTER);
 67: 
 68:             JPanel buttonPanel = new JPanel( );
 69:             buttonPanel.setLayout(new FlowLayout( ));
 70: 
 71:             JButton exitButton = new JButton("Yes");
 72:             exitButton.addActionListener(this);
 73:             buttonPanel.add(exitButton);
 74: 
 75:             JButton cancelButton = new JButton("No");
 76:             cancelButton.addActionListener(this);
 77:             buttonPanel.add(cancelButton);
 78: 
 79:             confirmContent.add(buttonPanel, BorderLayout.SOUTH);
 80:         }
 81: 
 82:         public void actionPerformed(ActionEvent e) 
 83:         {
 84:             if (e.getActionCommand( ).equals("Yes")) 
 85:                 System.exit(0);
 86:             else if (e.getActionCommand( ).equals("No"))
 87:                 dispose( );//Destroys only the ConfirmWindow.
 88:             else
 89:                 System.out.println("Error in Confirm Window.");
 90:         }
 91:     }
 92: 
 93:     public static void main(String[] args)
 94:     {
 95:         CloseWindowDemo gui = new CloseWindowDemo( );
 96:         gui.setVisible(true);
 97:     }
 98: }