240 likes | 344 Views
System development with Java. Instructors: Rina Zviel-Girshin Lecture 12. Overview. LayoutManagers FlowLayout BoarderLayout GridLayout. LayoutManager. The LayoutManager class lets you control the locations of individual components in Java applets.
E N D
System development with Java Instructors: Rina Zviel-Girshin Lecture 12 Rina Zviel-Girshin @ARC
Overview • LayoutManagers • FlowLayout • BoarderLayout • GridLayout Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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")); } } Rina Zviel-Girshin @ARC
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)); Rina Zviel-Girshin @ARC
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")); } } Rina Zviel-Girshin @ARC
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 ); Rina Zviel-Girshin @ARC
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")); } } Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
BorderLayout Rina Zviel-Girshin @ARC
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")); Rina Zviel-Girshin @ARC
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")); } } Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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")); } } Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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.) Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
Sun Example Rina Zviel-Girshin @ARC
Sun Example Rina Zviel-Girshin @ARC
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; } } Rina Zviel-Girshin @ARC
Any Questions? Rina Zviel-Girshin @ARC