Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 12."— Presentation transcript:

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

2 Rina Zviel-Girshin @ARC2 Overview LayoutManagers FlowLayout BoarderLayout GridLayout

3 Rina Zviel-Girshin @ARC3 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.

4 Rina Zviel-Girshin @ARC4 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.

5 Rina Zviel-Girshin @ARC5 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.

6 Rina Zviel-Girshin @ARC6 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.

7 Rina Zviel-Girshin @ARC7 Example import 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")); } }

8 Rina Zviel-Girshin @ARC8 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));

9 Rina Zviel-Girshin @ARC9 Example import 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")); } }

10 Rina Zviel-Girshin @ARC10 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 );

11 Rina Zviel-Girshin @ARC11 Hgap Example import 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")); } }

12 Rina Zviel-Girshin @ARC12 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.

13 Rina Zviel-Girshin @ARC13 BorderLayout

14 Rina Zviel-Girshin @ARC14 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"));

15 Rina Zviel-Girshin @ARC15 Example import 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")); } }

16 Rina Zviel-Girshin @ARC16 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.

17 Rina Zviel-Girshin @ARC17 Example import 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")); }

18 Rina Zviel-Girshin @ARC18 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.

19 Rina Zviel-Girshin @ARC19 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.)

20 Rina Zviel-Girshin @ARC20 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.

21 Rina Zviel-Girshin @ARC21 Sun Example

22 Rina Zviel-Girshin @ARC22 Sun Example

23 Rina Zviel-Girshin @ARC23 Example import 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; } }

24 Rina Zviel-Girshin @ARC24 Any Questions?


Download ppt "Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 12."

Similar presentations


Ads by Google