700 Graphical User Interface (Web hosting domain names) Components: Part 1 Chapter

700 Graphical User Interface Components: Part 1 Chapter 12 18 private Container container; 19 private GridLayout grid1, grid2; 20 21 // set up GUI 22 public GridLayoutDemo() 23 { 24 super( “GridLayout Demo” ); 25 26 // set up layouts 27 grid1 = new GridLayout( 2, 3, 5, 5 ); 28 grid2 = new GridLayout( 3, 2 ); 29 30 // get content pane and set its layout 31 container = getContentPane(); 32 container.setLayout( grid1 ); 33 34 // create and add buttons 35 buttons = new JButton[ names.length ]; 36 37 for ( int count = 0; count < names.length; count++ ) { 38 buttons[ count ] = new JButton( names[ count ] ); 39 buttons[ count ].addActionListener( this ); 40 container.add( buttons[ count ] ); 41 } 42 43 setSize( 300, 150 ); 44 setVisible( true ); 45 } 46 47 // handle button events by toggling between layouts 48 public void actionPerformed( ActionEvent event ) 49 { 50 if ( toggle ) 51 container.setLayout( grid2 ); 52 else 53 container.setLayout( grid1 ); 54 55 toggle = !toggle; // set toggle to opposite value 56 container.validate(); 57 } 58 59 // execute application 60 public static void main( String args[] ) 61 { 62 GridLayoutDemo application = new GridLayoutDemo(); 63 64 application.setDefaultCloseOperation( 65 JFrame.EXIT_ON_CLOSE ); 66 } 67 68 } // end class GridLayoutDemo Fig. 12.26 Program that demonstrates components in GridLayout. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply