Java Reference
In-Depth Information
tom. To do this, instead of adding the button to rightBox , create an invisible
component and add it:
Class Box has a number of static methods for generating components that create
space in one fashion or another. Study the class specification to find out more
about them. Here are some of them:
Box.createGlue(); Box.createRigidArea(…);
Box.VerticalGlue(); Box.createHorizontalGlue();
Box.createVerticalStrut(…); Box.createHorizontalStrut(…);
Be careful with these procedures, for their action when the JFrame is resized
is not always clear. When using these features, you might fix the JFrame so it
cannot be resized by the user:
setResizable( false );
Using different layout managers
It is possible to change the layout manager of a JFrame (but not to a BoxLayout
manager). For example, to change it to a flow layout, use:
Obtain a class
Demo from a
footnote on les-
son page 17-3.
getContentPane.setLayout( new FlowLayout());
BoxLayout managers are reserved only for Box objects. If you want to use a
BoxLayout manager for a JFrame , then create a Box and place it as the sole com-
ponent of the JFrame , as outlined here:
JFrame jf= new JFrame("title");
Box b= new Box(BoxLayout.X_AXIS);
Add components to b;
Listening to a GUI
A GUI is useful only when it reacts to events —clicking a button, typing in a text
field, etc. In order to react, the program must have access to the events. In this
section, we show how to listen to events. We concentrate on listening to buttons
and to mouse events, for those are the most widely used events.
page 17-4
Figure 17.13:
A JFrame with two buttons, in two different states
Search WWH ::

Custom Search