Source of CloseWindowDemo.java


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