Web hosting top - Chapter 12 Graphical User Interface Components: Part 1
Chapter 12 Graphical User Interface Components: Part 1 697 14 private JButton buttons[]; 15 private String names[] = { “Hide North”, “Hide South”, 16 “Hide East”, “Hide West”, “Hide Center” }; 17 private BorderLayout layout; 18 19 // set up GUI and event handling 20 public BorderLayoutDemo() 21 { 22 super( “BorderLayout Demo” ); 23 24 layout = new BorderLayout( 5, 5 ); 25 26 // get content pane and set its layout 27 Container container = getContentPane(); 28 container.setLayout( layout ); 29 30 // instantiate button objects 31 buttons = new JButton[ names.length ]; 32 33 for ( int count = 0; count < names.length; count++ ) { 34 buttons[ count ] = new JButton( names[ count ] ); 35 buttons[ count ].addActionListener( this ); 36 } 37 38 // place buttons in BorderLayout; order not important 39 container.add( buttons[ 0 ], BorderLayout.NORTH ); 40 container.add( buttons[ 1 ], BorderLayout.SOUTH ); 41 container.add( buttons[ 2 ], BorderLayout.EAST ); 42 container.add( buttons[ 3 ], BorderLayout.WEST ); 43 container.add( buttons[ 4 ], BorderLayout.CENTER ); 44 45 setSize( 300, 200 ); 46 setVisible( true ); 47 } 48 49 // handle button events 50 public void actionPerformed( ActionEvent event ) 51 { 52 for ( int count = 0; count < buttons.length; count++ ) 53 54 if ( event.getSource() == buttons[ count ] ) 55 buttons[ count ].setVisible( false ); 56 else 57 buttons[ count ].setVisible( true ); 58 59 // re-layout the content pane 60 layout.layoutContainer( getContentPane() ); 61 } 62 63 // execute application 64 public static void main( String args[] ) 65 { 66 BorderLayoutDemo application = new BorderLayoutDemo(); Fig. 12.25 Demonstrating components in BorderLayout(part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01