Java Reference
In-Depth Information
We can do much better than this. We can set constraints on the edges of the container using springs that
will control its size. We can therefore place constraints on the height and width of the container in terms
of the springs that we used to determine the size and locations of the components. This will have the
effect of relating all the springs that determine the size and position of the buttons to the size of the
application window. Try adding the following code to the example immediately preceding the call to
setVisible() for the window object:
SpringLayout.Constraints constr = layout.getConstraints(content);
constr.setConstraint(SpringLayout.EAST,
Spring.sum(buttonConstr.getConstraint(SpringLayout.EAST),
Spring.constant(15)));
constr.setConstraint(SpringLayout.SOUTH,
Spring.sum(buttonConstr.getConstraint(SpringLayout.SOUTH),
Spring.constant(10)));
aWindow.pack();
This sets the constraint on the EAST edge of a container that is the Spring constraining the EAST edge
of the last button plus a strut 15 units long. This positions the right edge of the container 15 units to the
right of the right edge of the last button. The bottom edge of the container is similarly connected by a
fixed link, 10 units long, to the bottom edge of the last button. If you recompile with these additions and
run the example again, you should find that not only is the initial size of the window set to
accommodate all the buttons, but also when you resize the window the size and positions of the buttons
adapt accordingly. Isn't that nice?
The SpringLayout manager is extremely flexible and can do much of what the other layout mangers
can do if you choose the constraints on the components appropriately. It's well worth experimenting to
see the effect of various configurations of springs on your application.
Adding a Menu to a Window
As we have already discussed, a JMenuBar object represents the menu bar that is placed at the top of a
window. You can add JMenu or JMenuItem objects to a JMenuBar object and these will be displayed
on the menu bar. A JMenu object is a menu item with a label that can display a pull-down menu when
clicked. A JMenuItem object represents a simple menu item with a label that results in some program
action when clicked - such as opening a dialog. A JMenuItem can have an icon in addition to, or
instead of, a String label. Each item on the pull-down menu for an object of type JMenu , can be an
object of either type JMenu , JMenuItem , JCheckBoxMenuItem , or JRadioButtonMenuItem .
A JCheckBoxMenuItem is a simple menu item with a checkbox associated with it. The checkbox can be
checked and unchecked and typically indicates that that menu item was selected last time the pull-down
menu was displayed. You can also add separators in a pull-down menu. These are simply bars to separate
one group of menu items from another. A JRadioButtonMenuItem is a menu item much like a radio
button in that it is intended to be one of a group of like menu items added to a ButtonGroup object. Both
JCheckBoxMenuItem and JRadioButtonMenuItem objects can have icons.
Search WWH ::




Custom Search