Chapter 12 Graphical User (Web hosting script) Interface Components: Part 1
Chapter 12 Graphical User Interface Components: Part 1 701 Fig. 12.26 Program that demonstrates components in GridLayout. Lines 27 28 in the constructor define two GridLayoutobjects. The GridLayout constructor used at line 27 specifies a GridLayoutwith 2rows, 3columns, 5 pixels of horizontal-gap space between Components in the grid and 5pixels of vertical-gap space between Components in the grid. The GridLayoutconstructor used at line 28 specifies a GridLayoutwith 3rows, 2 columns and no gap space. The JButtonobjects in this example initially are arranged using grid1(set for the content pane at line 32 with method setLayout). The first component is added to the first column of the first row. The next component is added to the second column of the first row, and so on. When a JButton is pressed, method actionPerformed (lines 48 57) is called. Every call to actionPerformedtoggles the layout between grid2and grid1. Line 56 illustrates another way to relayout a container for which the layout has changed. Container method validate recomputes the container s layout based on the current layout manager for the Containerand the current set of displayed GUI components. 12.15 Panels Complex GUIs (like Fig. 12.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. Panels are created with class JPanel a subclass of JComponent. Class JComponent inherits from class java.awt.Container, so every JPanel is a Container. Thus JPanels may have components, including other panels, added to them. The program of Fig. 12.27 demonstrates how a JPanelcan be used to create a more complex layout for Components. 1 // Fig. 12.27: PanelDemo.java 2 // Using a JPanel to help lay out components. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class PanelDemo extends JFrame { 12 private JPanel buttonPanel; Fig. 12.27 A JPanel with five JButtons in a GridLayoutattached to the SOUTHregion of a BorderLayout(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01