Java Reference
In-Depth Information
1
// Fig. 12.40: FlowLayoutDemo.java
2
// Testing FlowLayoutFrame.
3
import
javax.swing.JFrame;
4
5
public
class
FlowLayoutDemo
6
{
7
public
static
void
main(String[] args)
8
{
9
FlowLayoutFrame flowLayoutFrame =
new
FlowLayoutFrame();
10
flowLayoutFrame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE
);
11
flowLayoutFrame.setSize(
300
,
75
);
12
flowLayoutFrame.setVisible(
true
);
13
}
14
}
// end class FlowLayoutDemo
Fig. 12.40
|
Testing
FlowLayoutFrame
.
Each button's event handler is specified with a separate anonymous inner-class object
(lines 30-43, 48-61 and 66-79, respectively), and method
actionPerformed
in each case
executes two statements. For example, line 37 in the event handler for
leftJButton
uses
FlowLayout
method
setAlignment
to change the alignment for the
FlowLayout
to a left-
aligned (
FlowLayout.LEFT
)
FlowLayout
. Line 40 uses
LayoutManager
interface method
layoutContainer
(which is inherited by all layout managers) to specify that the
JFrame
should be rearranged based on the adjusted layout. According to which button was clicked,
the
actionPerformed
method for each button sets the
FlowLayout
's alignment to
Flow-
Layout.LEFT
(line 37),
FlowLayout.CENTER
(line 55) or
FlowLayout.RIGHT
(line 73).
The
BorderLayout
layout manager (the default layout manager for a
JFrame
) arranges
components into five regions:
NORTH
,
SOUTH
,
EAST
,
WEST
and
CENTER
.
NORTH
corresponds to
the top of the container. Class
BorderLayout
extends
Object
and implements interface
LayoutManager2
(a subinterface of
LayoutManager
that adds several methods for en-
hanced layout processing).
A
BorderLayout
limits a
Container
to containing
at most five components
—one in
each region. The component placed in each region can be a container to which other com-
ponents are attached. The components placed in the
NORTH
and
SOUTH
regions extend hor-