Web page design - Chapter 13 Graphical User Interface Components: Part 2
Chapter 13 Graphical User Interface Components: Part 2 767 13.13 BoxLayoutLayout Manager The BoxLayoutlayout manager arranges GUI components horizontally along the x-axis or vertically along the y-axis of a container. The program of Fig. 13.15 demonstrates Box- Layoutand the container class Boxthat uses BoxLayoutas its default layout manager. In the constructor for class BoxLayoutDemo, lines 19 20 obtain a reference to the content pane and set its layout to a BorderLayout with 30 pixels of horizontal and 30 pixels of vertical gap space between components. The space is to help isolate each of the containers with BoxLayoutin this example. Lines 23 28 define an array of Box container references called boxes and initialize each element of the array with Box objects. Elements 0 and 2 of the array are initialized with staticmethod createHorizontalBoxof class Box, which returns a Boxcontainer with a horizontal BoxLayout (GUI components are arranged left-to-right). Elements 1 and 3 of the array are initialized with static method createVerticalBox of class Box, which returns a Boxcontainer with a vertical BoxLayout(GUI components are arranged top-to-bottom). 1 // Fig. 13.15: BoxLayoutDemo.java 2 // Demonstrating BoxLayout. 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 BoxLayoutDemo extends JFrame { 12 13 // set up GUI 14 public BoxLayoutDemo() 15 { 16 super( “Demostrating BoxLayout” ); 17 final int SIZE = 3; 18 19 Container container = getContentPane(); 20 container.setLayout( new BorderLayout( 30, 30 ) ); 21 22 // create Box containers with BoxLayout 23 Box boxes[] = new Box[ 4 ]; 24 25 boxes[ 0 ] = Box.createHorizontalBox(); 26 boxes[ 1 ] = Box.createVerticalBox(); 27 boxes[ 2 ] = Box.createHorizontalBox(); 28 boxes[ 3 ] = Box.createVerticalBox(); 29 30 // add buttons to boxes[ 0 ] 31 for ( int count = 0; count < SIZE; count++ ) 32 boxes[ 0 ].add( new JButton( "boxes[0]: " + count ) ); Fig. 13.15 Demonstrating the BoxLayoutlayout manager (part 1 of 3).
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services