public class BorderLayoutFrame extends JFrame implements ActionListener
1: // Fig. 11.41: BorderLayoutFrame.java
2: // Demonstrating BorderLayout.
3: import java.awt.BorderLayout;
4: import java.awt.event.ActionListener;
5: import java.awt.event.ActionEvent;
6: import javax.swing.JFrame;
7: import javax.swing.JButton;
8:
9: public class BorderLayoutFrame extends JFrame implements ActionListener
10: {
11: private JButton buttons[]; // array of buttons to hide portions
12: private final String names[] = { "Hide North", "Hide South",
13: "Hide East", "Hide West", "Hide Center" };
14: private BorderLayout layout; // borderlayout object
15:
16: // set up GUI and event handling
17: public BorderLayoutFrame()
18: {
19: super( "BorderLayout Demo" );
20:
21: layout = new BorderLayout( 5, 5 ); // 5 pixel gaps
22: setLayout( layout ); // set frame layout
23: buttons = new JButton[ names.length ]; // set size of array
24:
25: // create JButtons and register listeners for them
26: for ( int count = 0; count < names.length; count++ )
27: {
28: buttons[ count ] = new JButton( names[ count ] );
29: buttons[ count ].addActionListener( this );
30: } // end for
31:
32: add( buttons[ 0 ], BorderLayout.NORTH ); // add button to north
33: add( buttons[ 1 ], BorderLayout.SOUTH ); // add button to south
34: add( buttons[ 2 ], BorderLayout.EAST ); // add button to east
35: add( buttons[ 3 ], BorderLayout.WEST ); // add button to west
36: add( buttons[ 4 ], BorderLayout.CENTER ); // add button to center
37: } // end BorderLayoutFrame constructor
38:
39: // handle button events
40: public void actionPerformed( ActionEvent event )
41: {
42: // check event source and layout content pane correspondingly
43: for ( JButton button : buttons )
44: {
45: if ( event.getSource() == button )
46: button.setVisible( false ); // hide button clicked
47: else
48: button.setVisible( true ); // show other buttons
49: } // end for
50:
51: layout.layoutContainer( getContentPane() ); // layout content pane
52: } // end method actionPerformed
53: } // end class BorderLayoutFrame
54:
55: /**************************************************************************
56: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
57: * Pearson Education, Inc. All Rights Reserved. *
58: * *
59: * DISCLAIMER: The authors and publisher of this book have used their *
60: * best efforts in preparing the book. These efforts include the *
61: * development, research, and testing of the theories and programs *
62: * to determine their effectiveness. The authors and publisher make *
63: * no warranty of any kind, expressed or implied, with regard to these *
64: * programs or to the documentation contained in these books. The authors *
65: * and publisher shall not be liable in any event for incidental or *
66: * consequential damages in connection with, or arising out of, the *
67: * furnishing, performance, or use of these programs. *
68: *************************************************************************/