Вы находитесь на странице: 1из 24

Layout Manager

• Layout Manager
– laying out your controls by hand
• First, it is very tedious to manually lay out a large number of components.
• Second, sometimes the width and height information is not yet available
when you need to arrange some control, because the native toolkit
components haven’t been realized.
• Container object has a layout manager associated.
– layout manager is an instance of any class that implements the
LayoutManager interface
– void setLayout(LayoutManager layoutObj)
– If not set, default layout manager is used
– Whenever a container is resized the layout manager is used to position each
of the components within it.
– If you wish to disable the layout manager and position components manually,
pass null for layoutObj.
– Use the setBounds( ) method defined by Component to determine the shape
and position of each component manually
– keeps track of a list of components that are stored by their names.
– notified each time you add a component to a container
position components manually
import java.awt.*; D:\java2016\Layout>javac NullLayoutManager.java
public class NullLayoutManager extends Frame
{ Button agreedBut, denyBut; D:\java2016\Layout>java NullLayoutManager
Label sign;
public NullLayoutManager()
{ super("No Layout Manager");
setLayout(null);
sign = new Label("Click Your Acceptance");
agreedBut = new Button("AGREED");
denyBut = new Button("DENIED");
sign.setBounds(80, 35, 130, 25);
agreedBut.setBounds(60, 75, 75, 25);
denyBut.setBounds(150, 75, 75, 25);
add(sign); add(agreedBut); add(denyBut);
setSize(260, 130);
setVisible(true);
}
public static void main(String args[])
{
new NullLayoutManager ();
}
}
import java.awt.*;
public class NullLayoutManager extends
Frame
{ Button agreedBut, denyBut;
Label sign;
public NullLayoutManager()
{ super("No Layout Manager");
setLayout(null);
sign = new Label("Click Your Acceptance");
agreedBut = new Button("AGREED");
denyBut = new Button("DENIED");
sign.setBounds(150, 35, 130, 25);
agreedBut.setBounds(60, 75, 150, 25);
denyBut.setBounds(250, 75, 75, 25);
add(sign); add(agreedBut); add(denyBut);
setSize(350, 130);
setVisible(true);
}
public static void main(String args[])
{
new NullLayoutManager ();
}
}
• Whenever the container needs to be resized,
– the layout manager is consulted via
» minimumLayoutSize( ) and preferredLayoutSize( )methods.
» Each component that is being managed by a layout manager contains
» the getPreferredSize( ) and getMinimumSize( ) methods.
– Flowlayout
• Default layout manager for applet
• similar to how words flow in a text editor
• Components are laid out from the upper-left corner, left to right and top to bottom
• Constructors
– FlowLayout( ) // place components centre leaves 5 pixels b/w comp
– FlowLayout(int how)
static int CENTER
This value indicates that each row of components should be centered.
static int LEFT
This value indicates that each row of components should be left-justified.
static int RIGHT
This value indicates that each row of components should be right-justified.

– FlowLayout(int how, int horz, int vert) // horz & vertical space b/w com
– setLayout(new FlowLayout(FlowLayout.LEFT));
//Applet default layout –FlowLayout.CENTER
import java.applet.*;
import java.awt.*;
/* <applet code="FlowLayoutDemo" width=300
height=300>
</applet>*/
public class FlowLayoutDemo extends Applet
{public void init()
{
for(int i=0;i<5;i++)
add(new Button(" "+i));
}
}
import java.applet.*;
import java.awt.*;
/* <applet code="FlowLayoutDemo" width=300
height=300>
</applet>*/
public class FlowLayoutDemo extends Applet
{ FlowLayout fl;
public void init()
{
fl=new FlowLayout(FlowLayout.LEFT,15,15);
setLayout(fl);
for(int i=0;i<5;i++)
add(new Button(" "+i));
}
}
import java.applet.*;
import java.awt.*;
/* <applet code="FlowLayoutDemo" width=300
height=300>
</applet>*/
public class FlowLayoutDemo extends Applet
{ FlowLayout fl;
public void init()
{
fl=new FlowLayout(FlowLayout.RIGHT,15,15);
setLayout(fl);
for(int i=0;i<5;i++)
add(new Button(" "+i));
}
}
import java.applet.*;
import java.awt.*;
/* <applet code="FlowLayoutDemo" width=300
height=300>
</applet>*/
public class FlowLayoutDemo extends Applet
{ FlowLayout fl;
public void init()
{
fl=new FlowLayout(FlowLayout.CENTER,15,15);
setLayout(fl);
for(int i=0;i<5;i++)
add(new Button(" "+i));
}
}
• BorderLayout
– has four narrow, fixed-width components at the edges and one
large area in the center
• north, south, east, west and centre
– Constructors
• BorderLayout( )
• BorderLayout(int horz, int vert)
– Constants
• BorderLayout.CENTER BorderLayout.SOUTH
• BorderLayout.EAST BorderLayout.WEST
• BorderLayout.NORTH
– Method
• void add(Component compObj, Object region);
– setLayout(new BorderLayout());
– add(new Button("This is across the top."),BorderLayout.NORTH);
public void
import java.awt.*; adjustmentValueChanged(AdjustmentEvent ae)
import java.applet.*; { if(ae.getAdjustable()==b1)
import java.awt.event.*; { i=b1.getValue(); b=1; repaint(); }
/* <applet code="BorderLayoutDemo" name="CAL" if(ae.getAdjustable()==b2)
width=300 height=300> { i=b2.getValue(); b=0; repaint(); }
</applet>*/ if(ae.getAdjustable()==b3)
public class BorderLayoutDemo extends Applet {i=b3.getValue(); b=1; repaint(); }
implements AdjustmentListener if(ae.getAdjustable()==b4)
{ Scrollbar b1,b2,b3,b4; { i=b4.getValue();b=0; repaint(); }
int i,b=1; }
public void init() public void paint(Graphics g)
{setLayout(new BorderLayout(250,50)); { if(b==1)
b1=new Scrollbar(Scrollbar.HORIZONTAL,1,2,1,100); g.drawOval(getHeight()/2,getWidth()/2,50+i,50);
b2=new Scrollbar(Scrollbar.VERTICAL,1,2,1,100); else
b3=new Scrollbar(Scrollbar.HORIZONTAL,1,2,1,100); g.drawOval(getHeight()/2,getWidth()/2,50,50+i);
b4=new Scrollbar(Scrollbar.VERTICAL,1,2,1,100); } }
add("North",b1);
add("South",b3);
add("West",b2);
add(b4,BorderLayout.EAST);
b1.addAdjustmentListener(this);
b2.addAdjustmentListener(this);
b3.addAdjustmentListener(this);
b4.addAdjustmentListener(this);
}
• Insets
– leave a small amount of space between the
container that holds your components and the
window that contains.
– Constructor
• Insets(int top, int left, int bottom, int right)
– Methods
• Insets getInsets( )
– top, left, bottom, and right specify the amount of space between
the container and its enclosing window
– public Insets getInsets() {
– return new Insets(10, 10, 10, 10);
–}
• GridLayout
– lays out components in a two-dimensional grid. When
you instantiate a GridLayout, you define the number
of rows and columns
– Constructors
• GridLayout( )
• GridLayout(int numRows, int numColumns )
• GridLayout(int numRows, int numColumns, int horz, int vert)
– Either numRows or numColumns can be zero.
– Specifying numRows as zero allows for unlimited-length columns.
– Specifying numColumns as zero allows forunlimited-length rows.
– setLayout(new GridLayout(n, n));
import java.applet.*;
import java.awt.event.*;
/* <applet code="GridLayoutDemo"
width=300 height=300>
</applet>*/
public class GridLayoutDemo extends
Applet
{ GridLayout fl;
public void init()
{ fl=new GridLayout(3,3,3,3);
setLayout(fl);
LayoutManager lm=getLayout();
System.out.println(lm);
for(int i=0;i<9;i++)
D:\javalab>appletviewer GridLayoutDemo.java
add(new Button(" "+i));
java.awt.GridLayout[hgap=3,vgap=3,rows=3,cols=3]
}
public Insets getInsets()
{ return new Insets(20,20,20,20);}
}
• CardLayout
– is unique among the other layout managers in that it stores
several different layouts.
– Each layout can be thought of as being on a separate index
card in a deck that can be shuffled so that any card is on top
at a given time.
• Can be useful for user interfaces with optional components that can
be dynamically enabled and disabled upon user input.
• You can prepare the other layouts and have them hidden,ready to be
activated when needed.
• Constructors:
– CardLayout( )
– CardLayout(int horz, int vert)
void first(Container)
Void last(Container)
Void last(Container)
Void next(Container)
void previous(Container )
Void show(container,String)
import java.awt.*;
import java.applet.*; public void actionPerformed(ActionEvent ae)
import java.awt.event.*; {
/* <applet code="CardLayoutDemo" width=300 if(ae.getSource()==b1)
height=300>
c.show(this,"2");
</applet>*/
if(ae.getSource()==b2)
public class CardLayoutDemo extends Applet
implements ActionListener { // c.show(this,"3");
{ Button b1,b2,b3,b4; c.next(this);
CardLayout c; }
Choice c1; if(ae.getSource()==b3)
TextArea t1; {
public void init() //c.show(this,"4");
{ c=new CardLayout(10,10); c.last(this);
setLayout(c); }
b1=new Button("b1"); if(ae.getSource()==b4)
b2=new Button("b2"); {
b3=new Button("b3"); //c.show(this,"1");
b4=new Button("b4"); c.first(this);
add(b1,"1"); }
add(b2,"2"); }
add(b3,"3"); }
add(b4,"4");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
• Panel (Secondary Containers)
– A concrete subclass of Container and doesn’t add any new
methods.
– may be thought of as a recursively nestable, concrete screen
component(example:panel may contain another panel)
– Panel is the superclass for Applet. When screen output is
directed to an applet, it is drawn on the surface of a Panel
object.
– A panel is a window that does not contain a title bar, menu
bar, or border
• This is why you don’t see these items when an applet is run inside a
browser
• When you run an applet using an applet viewer, the applet viewer
provides the title and border
• Other components can be added to a Panel object by its add( ) method
(inherited from Container). Once these components have been added,
you can position and resize them manually using the setLocation( ),
setSize( ), or setBounds( ) methods defined by Component.
Panel
The Panel is the container that doesn't contain title bar and menu bars. It can have other
components like button, textfield etc. panel2.add(b4);
PanelExample panel2.add(b5);panel2.add(b6);
import java.awt.*; panel2.add(b7);
public class PanelExample { panel1.setBackground(Color.green);
PanelExample() panel2.setBackground(Color.red);
{
f.add(panel1);
Frame f= new Frame("Panel Example");
Panel panel1=new Panel(); f.add(panel2);
Panel panel2=new Panel(); f.setSize(400,400);
f.setLayout(new FlowLayout()); f.setVisible(true);
panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
Button b1=new Button("Button 1"); }
Button b2=new Button("Button 2"); public static void main(String args[])
Button b3=new Button("Button 3"); {
new PanelExample();
panel2.setLayout(new GridLayout(2,2,15,15));
} }
Button b4=new Button("Button 4");
Button b5=new Button("Button 5");
Button b6=new Button("Button 6");
Button b7=new Button("Button 7");
panel1.add(b1); panel1.add(b2);panel1.add(b3);
• cards are typically held in an object of type
Panel
• panel must have CardLayout selected asits
layout manager
• cards that form the deck are also typically
objects of type Panel
– create a panel that contains the deck and a panel
for each card in the deck
– Add the components to the appropriate panel
– Finally, add this panel to the main applet panel
– provide some way for the user to select between
cards
• Common way is include a pushbutton in each card
• void add(Component panelObj, Object name);
– When card panels are added to a panel, they are
usually given a name
• Invoke one of the following method
– void first(Container deck)
• first card in the deck to be shown
– void last(Container deck)
– void next(Container deck)
– void previous(Container deck)
– void show(Container deck, String cardName)
FlowLayout
static int LEADING
This value indicates that each row of
components should be justified to the leading
edge of the container's orientation, for example,
to the left in left-to-right orientations.
static int TRAILING
This value indicates that each row of
components should be justified to the trailing
edge of the container's orientation, for example,
to the right in left-to-right orientations.
import java.awt.*; panel1.setBackground(Color.green);
public class PanelExample {
PanelExample()
panel2.setBackground(Color.red);
{ f.add(panel1);
Frame f= new Frame("Panel Example"); f.add(panel2);
Panel panel1=new Panel(); f.setSize(400,400);
Panel panel2=new Panel();
f.setLayout(new FlowLayout(FlowLayout.TRAILING));
f.setVisible(true);
panel1.setLayout(new
FlowLayout(FlowLayout.LEFT)); }
Button b1=new Button("Button 1");
Button b2=new Button("Button 2");
public static void main(String args[])
Button b3=new Button("Button 3"); {
new PanelExample();
panel2.setLayout(new FlowLayout(FlowLayout.LEFT)); }
}
Button b4=new Button("Button 4");
Button b5=new Button("Button 5");
Button b6=new Button("Button 6");
Button b7=new Button("Button 7");

panel1.add(b1); panel1.add(b2);panel1.add(b3);
panel2.add(b4);
panel2.add(b5);panel2.add(b6); panel2.add(b7);
import java.awt.*; panel1.setBackground(Color.green);
public class PanelExample {
PanelExample()
panel2.setBackground(Color.red);
{ f.add(panel1);
Frame f= new Frame("Panel Example"); f.add(panel2);
Panel panel1=new Panel(); f.setSize(400,400);
Panel panel2=new Panel();
f.setLayout(new FlowLayout(FlowLayout.LEADING));
f.setVisible(true);
panel1.setLayout(new
FlowLayout(FlowLayout.LEFT)); }
Button b1=new Button("Button 1");
Button b2=new Button("Button 2");
public static void main(String args[])
Button b3=new Button("Button 3"); {
new PanelExample();
panel2.setLayout(new FlowLayout(FlowLayout.LEFT)); }
}
Button b4=new Button("Button 4");
Button b5=new Button("Button 5");
Button b6=new Button("Button 6");
Button b7=new Button("Button 7");

panel1.add(b1); panel1.add(b2);panel1.add(b3);
panel2.add(b4);
panel2.add(b5);panel2.add(b6); panel2.add(b7);

Вам также может понравиться