Java Reference
In-Depth Information
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 when you resize the window the size and positions of the buttons adapt accordingly.
Isn't that nice?
Note how the width and height of each button is maintained as you resize the window, even when the
content pane is too small to accommodate all the buttons. This is because a
JButton
object returns a
maximum size that is the same as the preferred size, so the layout manager does not alter the width or the
height.
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 you know, 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 are displayed on the menu bar. A
JMenu
object
is a menu with a label that can display a list of menu items when clicked. A
JMenuItem
object represents a
menu item with a label within a menu that results in some program action when clicked — such as opening
a dialog. A
JMenuItem
object can have an icon in addition to, or instead of, a
String
label. Each item in
a
JMenu
object can be of type
JMenu
,
JMenuItem
,
JCheckBoxMenuItem
, or
JRadioButtonMenuItem
. If an
item in a menu is a
JMenu
object then it represents a second level of menu containing further menu items.
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 drop-down
menu was displayed. You can also add separators in a drop-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
JCheck-
BoxMenuItem
and
JRadioButtonMenuItem
objects can have icons.
Creating JMenu and JMenuItem
To create a menu you call a
JMenu
class constructor and pass a
String
object to it that specifies the label.
For example, to create a File menu you could write:
JMenu fileMenu = new JMenu("File");
The string that is passed as the argument is the text that appears.
Creating a menu item is much the same as creating a menu:
JMenuItem openMenu = new JMenuItem("Open");
The argument is the text that appears on the menu item.