Upload
fay-walker
View
220
Download
1
Embed Size (px)
Citation preview
University of Limerick 1
Software Architecture
Java Layout Managers
University of Limerick 2
Lecture Objectives Understanding the difference between
Containers and Components Understand what a Layout Managers is
Frames and components
menus
labels
windows (frames)
checkboxes
radio buttons
borders
buttons
Containers and Components Most of the GUI classes we deal with can be
classified as either– Containers : Objects capable of containing other
graphical objects– Components : Stand alone graphical widgets with
no containment ability These classes inherit from (J)Component or
Container respectively
Containers Containers are Swing components that hold
other components Containers can be nested (containers in
containers) Containers use a LayoutManager to
determine how to arrange the components A Swing frame hold a container in it’s content
pane
University of Limerick 6
Example Containers
JPanel– Panel is the simplest container class. A panel provides space
in which an application can attach any other component. The default layout manager for a Panel is FlowLayout.
Window– A Window object is a top-level window with no borders and
no menubar. JFrame
– A Frame is a top-level window with title and border; default layout is BorderLayout.
JDialog– A window that takes input from the user.
Swing: frame structure
JFrame title bar
content pane
More on Containers (1) Swing provides four useful top-level container
classes: JFrame, JApplet, JDialog & JWindow. JPanel’s are intermediate containers. They are
used to store other GUI objects, such as buttons, labels, textfields, etc.
Note there are other intermediate containers, such as TabbedPane, Toolbar, etc.
Components and Containers GUI Objects such as labels, buttons, textfields,
radiobuttons are consider atomic components…they DO NOT store other GUI objects within themselves.
Every top-level container contains an intermediate container, known as a content pane.
University of Limerick 10
Components Standard GUI controls like buttons, menus,
scrollbars, text fields and lists are typical components
Swing also supplies some complex controls like Color, File and Font choosers, spreadsheet-like tables and Windows Explorer-like hierarchical trees
More on Components So whenever you add an atomic component
or intermediate container to a top-level container (e.g. JFrame), add it to the content pane.
E.g. Incorrect– add(new JButton(“hello”));
Correct– getContentPane().add(new JButton(“hello”));
University of Limerick 12
Adding Components to Containers
Containers exist to hold components which create the UI experience for the user
If you don't care about the layout of the components in the container, adding them is simple
Container contentPane = getContentPane();JButton aButton = new JButton(“Start”);contentPane.add(aButton);
University of Limerick 13
Layout managers
Using a layout manager you can choose one of a small number of standard arrangments for subwindows
You create a “layout manager” object - an instance of a class that– keeps track of subwindows and their (minimum) sizes– chooses how to position them in the enclosing window
you associate this layout manager with the window
Arranging components
containers
components
ExampleJPanel bluePanel = new JPanel();bluePanel.setBackground(Color.blue);bluePanel.setOpaque(true);bluePanel.setPreferredSize(new Dimension(200, 200));
….JPanel normalPanel = new JPanel(new GridLayout(0, 2));JLabel blackLabel = new JLabel("Black Label");
blackLabel.setOpaque(true);blackLabel.setBackground(Color.black);blackLabel.setForeground(Color.white);
JLabel greenLabel = new JLabel("Green Label");greenLabel.setOpaque(true);greenLabel.setBackground(Color.green);
normalPanel.add(blackLabel);normalPanel.add(greenLabel);
Why Layout Managers So the programmer no longer bares the
burden of specifying the exact position and dimension of each component
Layout Managers each container manages layout of its
components the programmer just adds components, the
container looks after layout the container uses a Layout Manager to
handle the layout different layout managers are available layout can be specified by specifying layout
managers for containers
Layout Managers Layout managers
– FlowLayout– BorderLayout– GridLayout– GridBagLayout– CardLayout
FlowLayout
Arranges all components in a horizontal row Components flow into next line when no space in row The FlowLayout managers always honours a
components preferred size Default layout for JPanels
Flow Layout Managers getContentPane().setLayout(new FlowLayout(10, 30,
FlowLayout.LEFT));JButton JButNorth = new JButton("AAAA");JButton JButCenter = new JButton("BBB");JButton JButWest = new JButton("CCC");JButton JButEast = new JButton("DDDD");JButton JButSouth = new JButton("EEEE");
getContentPane().add(JButNorth);getContentPane().add(JButWest);getContentPane().add(JButSouth);getContentPane().add(JButEast);getContentPane().add(JButCenter);
BorderLayout
Content panes (main containers in JFrame, JDialog, JApplet) use BorderLayout by default)
Holds up to five components in fixed places
center
west
east
south
north
BorderLayout
The BorderLayout manager honours the preferred height of the North and South sectors but expands the width to fill the entire container
Honours the preferred width of the East and West sectors but expands the height to fill the entire container
Border Layout Managers ...
JButton JButNorth = new JButton("North");JButton JButCenter = new JButton("Center");JButton JButWest = new JButton("West");JButton JButEast = new JButton("East");JButton JButSouth = new JButton("South");
getContentPane().add(JButNorth, BorderLayout.NORTH);getContentPane().add(JButWest, BorderLayout.WEST);getContentPane().add(JButSouth, BorderLayout.SOUTH);getContentPane().add(JButEast, BorderLayout.EAST);getContentPane().add(JButCenter);
...
GridLayout
Arranges components in a grid The GridLayout managers ignores a components preferred
size– Components are expanded to fill the sector
Grid Layout Managers getContentPane().setLayout(new GridLayout(3, 3));
JButton JButNorth = new JButton("Click");JButton JButCenter = new JButton("Exit");JButton JButWest = new JButton("Save");JButton JButEast = new JButton("Bye");JButton JButSouth = new JButton("Hello");
getContentPane().add(JButNorth);getContentPane().add(JButWest);getContentPane().add(JButSouth);getContentPane().add(JButEast);getContentPane().add(JButCenter);
CardLayout
Arranges components in time rather than space Similar to a tabbed panel without the tabs Components are arranged in sequence and only one is
displayed at a time– N.B. A component may be a JPanel
GridBag Layout
Most powerful layout manager Divides layout into an array of cell but cell may have
different height and widths Considered the most difficult to use
Layout Managers By default every container has a layout
manager JPanel use FlowLayout by default Content panes (main containers in JFrame,
JApplet) use BorderLayout by default) To override the default layout for any
containers use the setLayout method or specify it during constructor
Remember for top level containers use getContentPane().setLayout(...).
Combining Layout managers Most moderately sophisticated UI designs are
too complex for the layout requirements to be satisfied by a single layout manager
So you will often need to combine two or more separate layout managers to achieve the desired affect overall
Combining Layout managers This is accomplished by using extra container
objects (JPanels) with different layout managers within a main container like a JFrame
It is also possible to specify that you do not want to employ a layout manager, and position subwindows yourself
University of Limerick 31
Calculator Example public CalculatorPanel() { setLayout(new BorderLayout()); display = new JTextField("0"); display.setEditable(false); add(display, "North"); JPanel p = new JPanel(); p.setLayout(new GridLayout(4, 4)); String buttons = "789/456*123-0.=+"; for (int i = 0; i < buttons.length(); i++) addButton(p, buttons.substring(i, i + 1)); add(p, "Center");}
New panel
Add new panel toexisting panel
Set layout forpanel