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