CS 235 - Week 6 Lecture - 2015-09-28
* you've used a default layout manager up until
now, FlowLayout
* layout manager: it is an object adding
a layer of abstraction to allow a programmer
to more easily layout components within
containers
* layout managers provide:
* rules for positioning components
within a container
* does some or most or all (depending
on the layout manager) of the work
in calculating size and position
and giving graphics directions
to components
* manages the task of repositioning
components if the container's
size and position changes
* a programmer can choose to use NO layout
manager (and take care of some/all of the
above themselves);
and, a programmer can write their own
layout manager (using "hooks" provided
in the API)
BUT there are also a number of layout
managers provided in the Java API,
(some more for direct programmer use,
and some designed for use by "drag-and-drop"
GUI-design programs)
* the Java AWT (package java.awt) includes 5 predefined
layout managers:
* FlowLayout - the default layout manager for
JPanel
* BorderLayout - the default layout manager
for JFrame
* GridLayout - for simple grid-based layouts
(even rows, even columns)
* CardLayout - (we'll be skipping - JTabbedPanes
are more convenient)
* GridBagLayout - for more intricate
grid-based layouts
* The Swing toolkit (package javax.swing) provides
three more:
* BoxLayout - useful for components in a
single column or a single row
* SpringLayout
* GroupLayout
^ these 2 tend to be used in drag-n-drop
GUI builders, and we won't be discussing
those here
* just a few more words on FlowLayout
* the centering of components within a
container "row" is actually a
FLowLayout default --
you can MODIFY that by calling its
setAlignment method
* step 1:
declare an instance of a layout manager
FlowLayout myFlowLayout = new FlowLayout();
* you can set the layout manager of a container
to a layout manager object of your choice
with setLayout
for example, within a JPanel subclass'
constructor, I might have:
this.setLayout(myFlowLayout);
* and to make all components be left-justified
per row instead of centered, in THAT container,
can say:
myFlowLayout.setAlignment(FlowLayout.LEFT);
* intro BorderLayout
* BorderLayout contains 5 fixed positions:
NORTH
SOUTH
EAST
WEST
CENTER
* BorderLayout typically expects to be called
with a 2-argument add method --
the first is the component being added (as usual),
the second is a named constant giving the position
that component within the calling
container
(for BorderLayout, if you use the 1-argument
add method, BorderLayout.CENTER is the
position assumed)
* do NOT add more than one component in a
region -- the last added will be the
one you get;
* (and for any layout manager, don't add
the same component more than once --
it will (I think) end up in the last place
you try to add it...)
* GridLayout
displays components within cells such
that all rows are one height,
all columns are one width
* from the Java 8 API: "The container is divided
into equal-sized rectangles, and one component
is placed in each rectangle"
* often, you call this with a 2-argument
constructor specifying how many rows
and how many columns
* ...(it IGNORES the number of columns
if you add more elements than
the grid size)
* from the Java 8 API: actually,
if both num rows and num columns
are given and non-zero, the number of
columns is ALWAYS ignored (!!) --
"the number of columns is determined
by the specified number of rows
and the total number of components
in the layout."
"Specifying the number of columns affects
the layout ONLY when the number of rows
is set to zero"...!
* then, when you add to a container
using GridLayout,
you use the 1-argument add method
again,
and the components are added
left-to-right, top-to-bottom
* also from the Java 8 API: actually, it turns
out that this depends on the container's
ComponentOrientation property -- JPanel's
is evidently horizontal and left-to-right.
If a container's ComponentOrientation property
is horizontal and right-to-left, and using
GridLayout, components will be added
right-to-left, top-to-bottom...!