public class BoxLayoutFrame extends JFrame
1: // Fig. 22.24: BoxLayoutFrame.java
2: // Demonstrating BoxLayout.
3: import java.awt.Dimension;
4: import javax.swing.JFrame;
5: import javax.swing.Box;
6: import javax.swing.JButton;
7: import javax.swing.BoxLayout;
8: import javax.swing.JPanel;
9: import javax.swing.JTabbedPane;
10:
11: public class BoxLayoutFrame extends JFrame
12: {
13: // set up GUI
14: public BoxLayoutFrame()
15: {
16: super( "Demonstrating BoxLayout" );
17:
18: // create Box containers with BoxLayout
19: Box horizontal1 = Box.createHorizontalBox();
20: Box vertical1 = Box.createVerticalBox();
21: Box horizontal2 = Box.createHorizontalBox();
22: Box vertical2 = Box.createVerticalBox();
23:
24: final int SIZE = 3; // number of buttons on each Box
25:
26: // add buttons to Box horizontal1
27: for ( int count = 0; count < SIZE; count++ )
28: horizontal1.add( new JButton( "Button " + count ) );
29:
30: // create strut and add buttons to Box vertical1
31: for ( int count = 0; count < SIZE; count++ )
32: {
33: vertical1.add( Box.createVerticalStrut( 25 ) );
34: vertical1.add( new JButton( "Button " + count ) );
35: } // end for
36:
37: // create horizontal glue and add buttons to Box horizontal2
38: for ( int count = 0; count < SIZE; count++ )
39: {
40: horizontal2.add( Box.createHorizontalGlue() );
41: horizontal2.add( new JButton( "Button " + count ) );
42: } // end for
43:
44: // create rigid area and add buttons to Box vertical2
45: for ( int count = 0; count < SIZE; count++ )
46: {
47: vertical2.add( Box.createRigidArea( new Dimension( 12, 8 ) ) );
48: vertical2.add( new JButton( "Button " + count ) );
49: } // end for
50:
51: // create vertical glue and add buttons to panel
52: JPanel panel = new JPanel();
53: panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS ) );
54:
55: for ( int count = 0; count < SIZE; count++ )
56: {
57: panel.add( Box.createGlue() );
58: panel.add( new JButton( "Button " + count ) );
59: } // end for
60:
61: // create a JTabbedPane
62: JTabbedPane tabs = new JTabbedPane(
63: JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT );
64:
65: // place each container on tabbed pane
66: tabs.addTab( "Horizontal Box", horizontal1 );
67: tabs.addTab( "Vertical Box with Struts", vertical1 );
68: tabs.addTab( "Horizontal Box with Glue", horizontal2 );
69: tabs.addTab( "Vertical Box with Rigid Areas", vertical2 );
70: tabs.addTab( "Vertical Box with Glue", panel );
71:
72: add( tabs ); // place tabbed pane on frame
73: } // end BoxLayoutFrame constructor
74: } // end class BoxLayoutFrame
75:
76: /**************************************************************************
77: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
78: * Pearson Education, Inc. All Rights Reserved. *
79: * *
80: * DISCLAIMER: The authors and publisher of this book have used their *
81: * best efforts in preparing the book. These efforts include the *
82: * development, research, and testing of the theories and programs *
83: * to determine their effectiveness. The authors and publisher make *
84: * no warranty of any kind, expressed or implied, with regard to these *
85: * programs or to the documentation contained in these books. The authors *
86: * and publisher shall not be liable in any event for incidental or *
87: * consequential damages in connection with, or arising out of, the *
88: * furnishing, performance, or use of these programs. *
89: *************************************************************************/