Using Panels to Manage More Complex Layouts
Complex GUIs (like Fig. 11.1) require that each component be placed in an exact location. They often consist of multiple panels, with each panel's components arranged in a specific layout. Class JPanel extends JComponent and JComponent extends class Container, so every JPanel is a Container. Thus, every JPanel may have components, including other panels, attached to it with Container method add. The application of Fig. 11.45 and Fig. 11.46 demonstrates how a JPanel can be used to create a more complex layout in which several JButtons are placed in the SOUTH region of a BorderLayout.
Figure 11.45. JPanel with five JButtons in a GridLayout attached to the SOUTH region of a BorderLayout.
(This item is displayed on pages 576 - 577 in the print version)
1 // Fig. 11.45: PanelFrame.java 2 // Using a JPanel to help lay out components. 3 import java.awt.GridLayout; 4 import java.awt.BorderLayout; 5 import javax.swing.JFrame; 6 import javax.swing.JPanel; 7 import javax.swing.JButton; 8 9 public class PanelFrame extends JFrame 10 { 11 private JPanel buttonJPanel; // panel to hold buttons 12 private JButton buttons[]; // array of buttons 13 14 // no-argument constructor 15 public PanelFrame() 16 { 17 super( "Panel Demo" ); 18 buttons = new JButton[ 5 ]; // create buttons array 19 buttonJPanel = new JPanel(); // set up panel 20 buttonJPanel.setLayout( new GridLayout( 1, buttons.length ) ); 21 22 // create and add buttons 23 for ( int count = 0; count < buttons.length; count++ ) 24 { 25 buttons[ count ] = new JButton( "Button " + ( count + 1 ) ); 26 buttonJPanel.add( buttons[ count ] ); // add button to panel 27 } // end for 28 29 add( buttonJPanel, BorderLayout.SOUTH ); // add panel to JFrame 30 } // end PanelFrame constructor 31 } // end class PanelFrame |
Figure 11.46. Test class for PanelFrame.
(This item is displayed on page 577 in the print version)
1 // Fig. 11.46: PanelDemo.java 2 // Testing PanelFrame. 3 import javax.swing.JFrame; 4 5 public class PanelDemo extends JFrame 6 { 7 public static void main( String args[] ) 8 { 9 PanelFrame panelFrame = new PanelFrame(); 10 panelFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 11 panelFrame.setSize( 450, 200 ); // set frame size 12 panelFrame.setVisible( true ); // display frame 13 } // end main 14 } // end class PanelDemo
|
After JPanel buttonPanel is declared in line 11 and created at line 19, line 20 sets buttonPanel's layout to a GridLayout of one row and five columns (there are five JButtons in array buttons). Lines 2327 add the five JButtons in array buttons to the JPanel in the loop. Line 26 adds the buttons directly to the JPanelclass JPanel does not have a content pane, unlike a JFrame. Line 29 uses the default BorderLayout to add buttonPanel to the SOUTH region. Note that the SOUTH region is as tall as the buttons on buttonPanel. A JPanel is sized to the components it contains. As more components are added, the JPanel grows (according to the restrictions of its layout manager) to accommodate the components. Resize the window to see how the layout manager affects the size of the JButtons.