Source of FlowLayoutFrame.java


  1: // Fig. 11.39: FlowLayoutFrame.java
  2: // Demonstrating FlowLayout alignments.
  3: import java.awt.FlowLayout;
  4: import java.awt.Container;
  5: import java.awt.event.ActionListener;
  6: import java.awt.event.ActionEvent;
  7: import javax.swing.JFrame;
  8: import javax.swing.JButton;
  9: 
 10: public class FlowLayoutFrame extends JFrame 
 11: {
 12:    private JButton leftJButton; // button to set alignment left
 13:    private JButton centerJButton; // button to set alignment center
 14:    private JButton rightJButton; // button to set alignment right
 15:    private FlowLayout layout; // layout object
 16:    private Container container; // container to set layout
 17:    
 18:    // set up GUI and register button listeners
 19:    public FlowLayoutFrame()
 20:    {
 21:       super( "FlowLayout Demo" );
 22: 
 23:       layout = new FlowLayout(); // create FlowLayout
 24:       container = getContentPane(); // get container to layout
 25:       setLayout( layout ); // set frame layout
 26: 
 27:       // set up leftJButton and register listener
 28:       leftJButton = new JButton( "Left" ); // create Left button
 29:       add( leftJButton ); // add Left button to frame
 30:       leftJButton.addActionListener(
 31: 
 32:          new ActionListener() // anonymous inner class
 33:          {  
 34:             // process leftJButton event  
 35:             public void actionPerformed( ActionEvent event )
 36:             {
 37:                layout.setAlignment( FlowLayout.LEFT );
 38: 
 39:                // realign attached components
 40:                layout.layoutContainer( container );
 41:             } // end method actionPerformed
 42:          } // end anonymous inner class
 43:       ); // end call to addActionListener
 44: 
 45:       // set up centerJButton and register listener
 46:       centerJButton = new JButton( "Center" ); // create Center button
 47:       add( centerJButton ); // add Center button to frame
 48:       centerJButton.addActionListener(
 49: 
 50:          new ActionListener() // anonymous inner class 
 51:          { 
 52:             // process centerJButton event  
 53:             public void actionPerformed( ActionEvent event )
 54:             {
 55:                layout.setAlignment( FlowLayout.CENTER );
 56: 
 57:                // realign attached components
 58:                layout.layoutContainer( container );
 59:             } // end method actionPerformed
 60:          } // end anonymous inner class
 61:       ); // end call to addActionListener
 62: 
 63:       // set up rightJButton and register listener
 64:       rightJButton = new JButton( "Right" ); // create Right button
 65:       add( rightJButton ); // add Right button to frame
 66:       rightJButton.addActionListener(
 67: 
 68:          new ActionListener() // anonymous inner class
 69:          {  
 70:             // process rightJButton event  
 71:             public void actionPerformed( ActionEvent event )
 72:             {
 73:                layout.setAlignment( FlowLayout.RIGHT );
 74: 
 75:                // realign attached components
 76:                layout.layoutContainer( container );
 77:             } // end method actionPerformed
 78:          } // end anonymous inner class
 79:       ); // end call to addActionListener
 80:    } // end FlowLayoutFrame constructor
 81: } // end class FlowLayoutFrame
 82: 
 83: 
 84: /**************************************************************************
 85:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 86:  * Pearson Education, Inc. All Rights Reserved.                           *
 87:  *                                                                        *
 88:  * DISCLAIMER: The authors and publisher of this book have used their     *
 89:  * best efforts in preparing the book. These efforts include the          *
 90:  * development, research, and testing of the theories and programs        *
 91:  * to determine their effectiveness. The authors and publisher make       *
 92:  * no warranty of any kind, expressed or implied, with regard to these    *
 93:  * programs or to the documentation contained in these books. The authors *
 94:  * and publisher shall not be liable in any event for incidental or       *
 95:  * consequential damages in connection with, or arising out of, the       *
 96:  * furnishing, performance, or use of these programs.                     *
 97:  *************************************************************************/