Please send questions to
st10@humboldt.edu .
/**
* TryBox1
*
* a simple JApplet that includes several Box-related and
* BoxLayout-related examples.
*
* Modified from class BoxLayoutPane and its discussion, in Flanagan's
* "Java Examples in a Nutshell", 2nd Edition, pp. 194-196.
*
* RECOMMENDED SIZE: 300h x 400w
*
* modified by: Sharon M. Tuttle
* last modified: 2-26-01
**/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class TryBox1 extends JApplet
{
Container myContentPane;
public void init()
{
JButton paneButton;
JPanel panePanel;
Box topRowBox, bottomRowBox;
JPanel colPanel;
Box fixedColBox, fixedRowBox;
JTextArea textArea;
myContentPane = getContentPane();
// remember: JApplet's default layout is BorderLayout;
// from Flanagan, "Java Examples in a Nutshell", 2nd Ed., p. 194:
// "javax.swing.BoxLayout is a simple but versatile layout manager
// that arranges its children into a row or a column. The
// javax.swing.Box container uses BoxLayout; it is much more
// common to work with the Box class than to use BoxLayout
// directly. What gives Box containers their versatility is the
// ability to add stretchy space (glue) and rigid space (struts)
// to the layout."
/**********************************************************/
/* use a Box object to "add a plain row of buttons along */
/* the top" of the applet */
/**********************************************************/
// createHorizontalBox(): to create a "row" of something?
topRowBox = Box.createHorizontalBox(); // no new?! odd; but then, it
// is not calling a constructor,
// either;
// Box has BoxLayout as its default layout manager...
// create and add 5 buttons to this box
for (int i=0; i<5; i++)
{
JButton b = new JButton("B" + i);
b.setBackground(Color.red);
topRowBox.add(b);
}
// now add the box to the North of the applet
myContentPane.add(topRowBox, "North");
/**************************************************************/
/* this time, we'll use the BoxLayout manager with a non-Box */
/* component --- "add a plain column of buttons along the */
/* right edge". */
/* one reason why: here, we'll "give the column a border; */
/* CAN'T do this with the Box class." (!) */
/**************************************************************/
colPanel = new JPanel();
// notice what BoxLayout() constructor below requires: the
// component using the layout, followed by a constant
// indicating if this is horizontal or vertical:
colPanel.setLayout(new BoxLayout(colPanel, BoxLayout.Y_AXIS));
// another border example
colPanel.setBorder(new TitledBorder(new EtchedBorder(), "Column"));
// create and add 5 buttons
for (int i=0; i<5; i++)
{
JButton b = new JButton("B" + i);
b.setBackground(Color.yellow);
colPanel.add(b);
}
// and, of course, add the newly-filled panel to the
// RHS (East side) of the applet
myContentPane.add(colPanel, "East");
/******************************************************************/
/* "Add a button box along the bottom of the..." applet. "... */
/* "Add 'Glue' to space the buttons evenly. */
/******************************************************************/
bottomRowBox = Box.createHorizontalBox();
// adding stretchy space as the first thing in the box
// how do I create "glue"? With createHorizontalGlue()
// (or createVerticalGlue(), in a vertical box...)
bottomRowBox.add(Box.createHorizontalGlue());
bottomRowBox.add(new JButton("Left")); // a button
bottomRowBox.add(Box.createHorizontalGlue()); // more stretchy space
bottomRowBox.add(new JButton("Middle")); // a 2nd button
bottomRowBox.add(Box.createHorizontalGlue()); // more stretchy space
bottomRowBox.add(new JButton("Right")); // a 3rd button
bottomRowBox.add(Box.createHorizontalGlue()); // more stretchy space
// add the newly-filled box to the south of the applet
myContentPane.add(bottomRowBox, "South");
/****************************************************************/
/* a "strut" appears to be rigid space of a certain pixel size; */
/* Here, we will "use Box objects to give the JTextArea an */
/* unusual spacing." */
/* One box will be within another --- and note that the strut */
/* essentially appears as another "thing" that can be added to */
/* a box. */
/****************************************************************/
// first: here is a JTextArea to display somewhere in the center
// of the applet
textArea = new JTextArea();
textArea.setText("This JTextArea component has a 12-pixel \"margin\""
+ " on top, a 24-pixel \"margin\" on the left,"
+ " a 48-pixel \"margin\" on the right, and a"
+ " 72-pixel \"margin\""
+ " on the bottom.");
// AWT TextArea does *not* have these methods...
// (you can say how many spaces should make up a tab
// for a JTextArea using setTabSize(), too!)
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
// second: textArea goes as the second "column" in a column-oriented
// box --- note that above and below it are two struts;
fixedColBox = Box.createVerticalBox();
// create a "strut" of 12 "rigid" pixels
fixedColBox.add(Box.createVerticalStrut(12));
// component here will "fill in" the rest!
fixedColBox.add(textArea);
// finally, create a "strut" of 72 "rigid" pixels
fixedColBox.add(Box.createVerticalStrut(72));
// third: now, put fixedColBox onto a horizontal box, itself
// with two more struts;
fixedRowBox = Box.createHorizontalBox();
fixedRowBox.add(Box.createHorizontalStrut(24)); // "24 rigid pixels"
fixedRowBox.add(fixedColBox);
fixedRowBox.add(Box.createHorizontalStrut(48)); // "48 rigid pixels"
// finally: add the fixedRowBox to the applet!
myContentPane.add(fixedRowBox, "Center");
// how about a temporary button in the West, to see its
// affect on the textarea above?
//JButton looky = new JButton("looky");
//looky.setBackground(Color.green);
//myContentPane.add(looky, "West");
}
}