702 Graphical User Interface Components: Part 1 Chapter (Web site templates)

702 Graphical User Interface Components: Part 1 Chapter 12 13 private JButton buttons[]; 14 15 // set up GUI 16 public PanelDemo() 17 { 18 super( “Panel Demo” ); 19 20 // get content pane 21 Container container = getContentPane(); 22 23 // create buttons array 24 buttons = new JButton[ 5 ]; 25 26 // set up panel and set its layout 27 buttonPanel = new JPanel(); 28 buttonPanel.setLayout( 29 new GridLayout( 1, buttons.length ) ); 30 31 // create and add buttons 32 for ( int count = 0; count < buttons.length; count++ ) { 33 buttons[ count ] = 34 new JButton( “Button ” + ( count + 1 ) ); 35 buttonPanel.add( buttons[ count ] ); 36 } 37 38 container.add( buttonPanel, BorderLayout.SOUTH ); 39 40 setSize( 425, 150 ); 41 setVisible( true ); 42 } 43 44 // execute application 45 public static void main( String args[] ) 46 { 47 PanelDemo application = new PanelDemo(); 48 49 application.setDefaultCloseOperation( 50 JFrame.EXIT_ON_CLOSE ); 51 } 52 53 } // end class PanelDemo Fig. 12.27 A JPanel with five JButtons in a GridLayoutattached to the SOUTHregion of a BorderLayout(part 2 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply