Source of InnerClassDemo2.java


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