24
Rina Zviel-Girshin @AR C 1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Embed Size (px)

Citation preview

Page 1: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 1

System development with Java

Instructors: Rina Zviel-Girshin

Lecture 12

Page 2: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 2

Overview

• LayoutManagers

• FlowLayout

• BoarderLayout

• GridLayout

Page 3: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 3

LayoutManager

• The LayoutManager class lets you control the locations of individual components in Java applets.

• Since you're never sure how big an area you'll have to work with or how it will be shaped, most of the controls are relative in nature.

• java.awt.LayoutManager is an interface that defines the interface for classes that know how to lay out Containers.

Page 4: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 4

LayoutManager

• Five classes in the java packages implement java.awt.LayoutManager:– FlowLayout

– BorderLayout

– CardLayout

– GridLayout

– GridBagLayout

• In simple applets with just a few components you often need only one layout manager.

Page 5: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 5

FlowLayout

• A FlowLayout arranges components from left to right until there's no more space left.

• Then it begins a row lower and moves from left to right again.

• It is a default layout.• Each component in a FlowLayout gets as much

space as it needs and no more. • A FlowLayout is useful for laying out buttons but

not for much else.

Page 6: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 6

FlowLayout• Syntax:

FlowLayout fl = new FlowLayout();• You tell an applet to use a particular LayoutManager

instance by passing the object to the applet's setLayout() method like this:

this.setLayout(fl); Or in one line: this.setLayout(new FlowLayout());• Most of the time setLayout() is called in the init()

method.

Page 7: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 7

Exampleimport java.awt.*;import java.applet.*;

public class FLExample extends Applet{

public void init(){this.setLayout(new FlowLayout());

this.add( new Button("First")); this.add( new Button("Second")); this.add( new Button("Third")); }}

Page 8: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 8

Alignment• You can change the alignment of a FlowLayout in

the constructor. • Components are normally centered in an applet.• You can make them left or right justified instead.• To do this just pass one of the defined constants:

– FlowLayout.LEFT, – FlowLayout.RIGHT– FlowLayout.CENTER

• Example: this.setLayout(new FlowLayout(FlowLayout.RIGHT));

Page 9: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 9

Exampleimport java.awt.*;import java.applet.*;

public class FLExampleLeft extends Applet{

public void init() {this.setLayout(new FlowLayout(FlowLayout.LEFT));

this.add( new Button("First")); this.add( new Button("Second")); this.add( new Button("Third")); }}

Page 10: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 10

Separating components

• Most LayoutManagers allow you to control the minimum amount of vertical and horizontal space between different components.

• In FlowLayout you may pass the horizontal and vertical space as arguments (in pixels):

FlowLayout(int alignment, int hgap, int vgap);• Also you have :

– setHgap(int); – setVgap(int );

Page 11: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 11

Hgap Exampleimport java.awt.*;import java.applet.*;

public class FLExampleLeft1 extends Applet{

public void init(){FlowLayout fl=new FlowLayout(FlowLayout.LEFT);this.setLayout(fl);fl.setHgap(50);

this.add( new Button("First")); this.add( new Button("Second")); this.add( new Button("Third")); } }

Page 12: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 12

BorderLayout• A BorderLayout organizes an applet into 5

rectangular areas:– North, – South, – East, – West and – Center

• Each area is continually resized to fit the sizes of the components included in them.

• Center is whatever's left over in the middle.

Page 13: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 13

BorderLayout

Page 14: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 14

BorderLayout• There's no centering, left alignment, or right

alignment in a BorderLayout. • You can add horizontal and vertical gaps between

the areas.

this.setLayout(new BorderLayout(5, 10)); • To add components to a BorderLayout include the

name of the section you wish to add them to like:

this this.add("South", new Button("Start"));

Page 15: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 15

Exampleimport java.applet.*; import java.awt.*;

public class BLExample extends Applet {

public void init() { this.setLayout(new BorderLayout(20, 10)); this.add("North", new Button("North")); this.add("South", new Button("South")); this.add("Center", new Button("Center")); this.add("East", new Button("East")); this.add("West", new Button("West")); }}

Page 16: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 16

GridLayout

• A GridLayout divides an applet into a specified number of rows and columns which form a grid of cells, each equally sized and spaced.

• As Components are added to the layout they are placed in the cells, starting at the upper left hand corner and moving to the right and down the page.

• Each component is sized to fit into its cell. • This tends to squeeze and stretch components

unnecessarily.

Page 17: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 17

Exampleimport java.awt.*; import java.applet.Applet;

public class GLExample extends Applet { public void init() { setLayout(new GridLayout(3,2)); add(new Button("1")); add(new Button("2")); add(new Button("3")); add(new Button("4")); add(new Button("5")); add(new Button("6")); } }

Page 18: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 18

GridBagLayout

• GridBagLayout is the most precise of the five awt LayoutManagers.

• Each GridBagLayout uses a rectangular grid of cells, but each component can occupy one or more cells of the layout.

• It's similar to the GridLayout, but components do not need to be the same size.

• Furthermore components are not necessarily placed in the cells beginning at the upper left-hand corner and moving to the right and down.

Page 19: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 19

GridBagLayout

• The GridBagLayout constructor is trivial, GridBagLayout() with no arguments. GridBagLayout gbl = new GridBagLayout();

• Unlike the GridLayout() constructor, this does not say how many rows or columns there will be.

• If you put a component in row 8 and column 2, then Java will make sure there are at least nine rows and three columns. (Rows and columns start counting at zero.)

Page 20: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 20

CardLayout

• A CardLayout breaks the applet into a deck of cards, each of which has its own LayoutManager.

• Only one card appears on the screen at a time. • The user flips between cards, each of which shows

a different set of components. • In Java this might be used for a series of data input

screens, where more input is needed than can comfortably be fit on one screen.

Page 21: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 21

Sun Example

Page 22: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 22

Sun Example

Page 23: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 23

Exampleimport java.awt.*;

public class Card extends java.applet.Applet { CardLayout cards = new CardLayout(); public void init() { setLayout( cards ); add("one", new Button("one") ); add("two", new Button("two") ); add("three", new Button("three") ); } public boolean action( Event e, Object arg){ cards.next( this); return true; } }

Page 24: Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 12

Rina Zviel-Girshin @ARC 24

Any Questions?