=====
CS 235 - Week 7 Lecture - 2021-10-04
=====
=====
TODAY WE WILL
=====
* announcements
* intro to layout managers
* prep for next class
* Currect reading:
Course text - Chapter 11 - Section 11.2 - intro to layout management
* Should be starting Homework 5 -- due by 11:59 pm on Friday, October 8th
* other important reminders:
* Friday, October 8 - NO LAB - intructor will be at a conference!
* (but Homework 5 is due, 11:59 pm)
* Monday, October 11 - REVIEW for Exam 1
* turn in any outstanding homework bits by 11:59 pm
on TUESDAY, October 12th,
so example homework solutions can be made available early
on Wednesday, October 13th
* Friday, October 15 - EXAM 1 - given on Canvas during Lab Time,
you can take it in BSS 317 or somewhere with Internet
(Homework 6 will come out AFTER Exam 1)
* a layout manager is an object adding a LAYER of ABSTRACTION to allow
us to more easily lay out various components within various
containers
* package java.awt provides several classic predefined
layout manager classes, including:
* FlowLayout
* BorderLayout
* GridLayout
and package javax.swing provides a few more, including:
* BoxLayout
* you CAN use NO layout management, if you wish (and do it all
yourself)
...and you can also subclass a layout manager class
and write your own layout manager class, if you wish!
* AND different panels in a single container can use
different layout managers (convenient!)
* important tradeoff to keep in mind, with layout managers:
generally you tradeoff between EASE and CONTROL
* just a few words more about FlowLayout:
* default layout manager for MANY (not all) containers
(including JPanel!)
(but NOT for JFrame -- its default is BorderLayout)
* positions components in the order they are added to the
container, top-to-bottom, left-to-right
and once the components that will fit on a "row" are determined,
they are centered IF the default ALIGNMENT is being used
* OK, then -- how can you set the layout manager for a container
object?
...call its setLayout method, one of whose versions expects
a layout manager object
* and you can call methods of that layout management object
to affect how it lays things out
* for example, FlowLayout objects have a setAlignment method,
called with lovely named constants such as
FlowLayout.LEFT,
FlowLayout.RIGHT,
FlowLayout.CENTER ...to specify alignment within a "row"
=====
BorderLayout
=====
* contains 5 fixed positions,
BorderLayout.NORTH
BorderLayout.SOUTH
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.CENTER
* when you use a 2-argument add method
with a container using this,
the second argument is expected to be one of the
above constants,
and the component is placed in a region specified,
typically "filling" it
(if you use the 1-argument add method for that container,
that is using BorderLayout, it is assumed you mean to add it
to the center)
* [NOTE -- it is MORE COMMON, with BorderLayout,
to add a JPanel to a region, rather than a non-container
component...]
* BEWARE -- if you add two or more components to the same BorderLayout
region, you'll only see the LATEST one added...!
======
GridLayout
======
* not to be confused with GridBagLayout...!
* GridLayout displays components within a grid...!
within cells of "standard-sized" columns and rows
* in one of its constructors, you give the desired number
of rows and a desired number of columns,
and as you call the container's 1-argument add constructor,
this layout manager adds them to that grid
left-to-right, top-to-bottom (so-called row-major order)
* but, fun fact?!
from: Java 16 API, specifically:
https://docs.oracle.com/en/java/javase/16/docs/api/java.desktop/java/awt/GridLayout.html
"When both the number of rows and the number of columns
have been set to non-zero values, either by a constructor or
by the setRows and setColumns methods,
the number of columns specified is ignored." [!!]
"Instead, the number of columns is determined from the
specified number of rows and the total number of components
in the layout." [!!]
"So, for example, if three rows and two columns have been
specified and nine components are added to the layout, they
will be displayed as three rows of three columns."
"Specifying the number of columns affects the layout only
when the number of rows is set to zero." [!!]