Home » » Layout in Java Program

Layout in Java Program

Layout in Java Program

Layout in Java Program

Layout in Java Program

1.Flow Layout
2.Border Layout
3.Grid Layout
4.Card Layout

Flow Layout

import java.awt.*;
import java.applet.*;
public class FlowDemo extends Applet
{
void void init()
{
setLayout(new FlowLayout());
add(new Button("one"));
add(new Button("two"));
add(new Button("three"));
add(new Button("four"));
}
}
/*applet code=FlowDemo.class width=300 height=300></applet>*/

Border Layout

import java.awt.*;
class BorderLayoutExample extends Frame
{
BorderLayoutExample()
{
setLayout(new BorderLayout());
add(new Button("NORTH"),BorderLayout.NORTH);
add(new Button("SOUTH"),BorderLayout.SOUTH);
add(new Button("EAST"),BorderLayout.EAST);
add(new Button("WEST"),BorderLayout.WEST);
add(new Button("CENTER"),BorderLayout.CENTER);
}
class BorderLayout.JavaExample
{
public Static void main(String args[])
{
BorderLayoutExample frame=new BorderLayoutExample();
frame.setTitle("BorderLayout in java example");
frame.setSize(400,150);
frame.setVisible(true);
}

}

Grid Layout

import java.awt.*;
import java.applet.*;
public class Flow extends Applet
{
public void init()
{
setLayout(new GridLayout(3,3,5,5));
for(int i=1;i<=12;i++)
add(new Button("java"+i));
}
}

//<applet code=flow.class width=400 height=400></applet>

Card Layout

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class CardDemo extends Applet implements ActionListener
{
Button b1,b2,b3,b4,b5,b6,b7,b8,b9;
Panel p1,p2,c1,c2,c3;
CardLayout cd;
public void init()
{
setLayout(new FlowLayout());
b1=new Button("one");
b2=new Button("two");
b3=new Button("three");
b4=new Button("four");
b5=new Button("five");
b6=new Button("six");
b7=new Button("card 1");
b8=new Button("card 2");
b9=new Button("card 3");
p1=new Panel();
p1.add(b7);
p1.add(b8);
p1.add(b9);
p2=new Panel();
cd=new CardLayout();
p2.setLayout(cd);
c1=new Panel();
c1.setBackground(Color.red);
c1.add(b1);
c1.add(b2);
p2.add("card1",c1);
c2=new Panel();
c2.setBackground(color,.cyan);
c2.add(b3);
c2.add(b4);
p2.add("card2",c2);
c3=new Panel();
c3=setBackground(color.blue);
c3.add(b5);
c3.add(b6);
p2.add("card3",c3);
add(p1,"west");
add(p2,"center");
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
}
public void actonPerformed(ActionEvent ae)
{
if(ae.getSource()==b7)
cd.show(p2,"card1");
else if(ae.getSource()==b8)
cd.show(p2,"card2");
else
cd.show("p2,"card3");
}
}

/*<applet code="CardDemo.class" width=300 height=300></applet>*/

0 coment�rios:

Post a Comment

Link you don't want to vouch for Read more: https://html.com/seo/#ixzz5zVltiZ5g