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.

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net