Source of GridLayoutFrame.java


  1: // Fig. 11.43: GridLayoutFrame.java
  2: // Demonstrating GridLayout.
  3: import java.awt.GridLayout;
  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 GridLayoutFrame extends JFrame implements ActionListener 
 11: {
 12:    private JButton buttons[]; // array of buttons
 13:    private final String names[] = 
 14:       { "one", "two", "three", "four", "five", "six" };
 15:    private boolean toggle = true; // toggle between two layouts
 16:    private Container container; // frame container
 17:    private GridLayout gridLayout1; // first gridlayout
 18:    private GridLayout gridLayout2; // second gridlayout
 19: 
 20:    // no-argument constructor
 21:    public GridLayoutFrame()
 22:    {
 23:       super( "GridLayout Demo" );
 24:       gridLayout1 = new GridLayout( 2, 3, 5, 5 ); // 2 by 3; gaps of 5
 25:       gridLayout2 = new GridLayout( 3, 2 ); // 3 by 2; no gaps
 26:       container = getContentPane(); // get content pane
 27:       setLayout( gridLayout1 ); // set JFrame layout
 28:       buttons = new JButton[ names.length ]; // create array of JButtons
 29: 
 30:       for ( int count = 0; count < names.length; count++ )
 31:       {
 32:          buttons[ count ] = new JButton( names[ count ] );
 33:          buttons[ count ].addActionListener( this ); // register listener
 34:          add( buttons[ count ] ); // add button to JFrame
 35:       } // end for
 36:    } // end GridLayoutFrame constructor
 37: 
 38:    // handle button events by toggling between layouts
 39:    public void actionPerformed( ActionEvent event )
 40:    { 
 41:       if ( toggle )
 42:          container.setLayout( gridLayout2 ); // set layout to second
 43:       else
 44:          container.setLayout( gridLayout1 ); // set layout to first
 45: 
 46:       toggle = !toggle; // set toggle to opposite value
 47:       container.validate(); // re-layout container
 48:    } // end method actionPerformed
 49: } // end class GridLayoutFrame
 50: 
 51: /**************************************************************************
 52:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 53:  * Pearson Education, Inc. All Rights Reserved.                           *
 54:  *                                                                        *
 55:  * DISCLAIMER: The authors and publisher of this book have used their     *
 56:  * best efforts in preparing the book. These efforts include the          *
 57:  * development, research, and testing of the theories and programs        *
 58:  * to determine their effectiveness. The authors and publisher make       *
 59:  * no warranty of any kind, expressed or implied, with regard to these    *
 60:  * programs or to the documentation contained in these books. The authors *
 61:  * and publisher shall not be liable in any event for incidental or       *
 62:  * consequential damages in connection with, or arising out of, the       *
 63:  * furnishing, performance, or use of these programs.                     *
 64:  *************************************************************************/