0% found this document useful (0 votes)
19 views8 pages

Explayout

The document contains code examples demonstrating different layout managers in Java - FlowLayout, BorderLayout, GridLayout, and CardLayout. Each code sample creates a Frame with buttons or labels added using the specific layout manager. The FlowLayout example centers buttons horizontally. The BorderLayout example adds buttons and labels to different border positions (north, south, etc.). The GridLayout example displays buttons in a 3x3 grid. The CardLayout example switches between buttons added to the frame.

Uploaded by

vinisha.b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views8 pages

Explayout

The document contains code examples demonstrating different layout managers in Java - FlowLayout, BorderLayout, GridLayout, and CardLayout. Each code sample creates a Frame with buttons or labels added using the specific layout manager. The FlowLayout example centers buttons horizontally. The BorderLayout example adds buttons and labels to different border positions (north, south, etc.). The GridLayout example displays buttons in a 3x3 grid. The CardLayout example switches between buttons added to the frame.

Uploaded by

vinisha.b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Enrollment Number: FCOW21706

Input:
FlowLayout
package demo;
import java.awt.*;
public class Layout {
public static void main(String args[]) throws Exception {
Frame f = new Frame();
FlowLayout fl = new FlowLayout(FlowLayout.CENTER);
Button b1 = new Button("Happy");
Button b2 = new Button("Sad");
Button b3 = new Button("Lonely");
Button b4 = new Button("Cheerful");
Button b5 = new Button("JOY");
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.setLayout(fl);

f.setSize(300, 300);
f.setVisible(true);
}}
Output:
Border Layout:

package demo;
import java.awt.*;
public class Layout
{
public static void main(String[] args) throws Exception
{
Frame f = new Frame();
Button b1 = new Button("NORTH");
Button b2 = new Button("SOUTH");
Label l3 = new Label("EAST");
Label l4 = new Label("WEST");
Button t5 = new Button("CENTER");
f.add(b1, BorderLayout.NORTH);
f.add(b2, BorderLayout.SOUTH);
f.add(l3, BorderLayout.EAST);
f.add(l4, BorderLayout.WEST);

f.add(t5, BorderLayout.CENTER);
f.setSize(300, 300);
f.setVisible(true);
}}
Output:
GridLayout:

package demo;
import java.awt.*;
public class Layout
{
public static void main(String[] args) throws Exception {
Frame f = new Frame();
GridLayout gl = new GridLayout(3, 3);
Button b1 = new Button("One");
Button b2 = new Button("Two");
Button b3 = new Button("Three");
Button b4 = new Button("Four");
Button b5 = new Button("Five");
Button b6 = new Button("Six");
Button b7 = new Button("Seven");
Button b8 = new Button("Eight");
Button b9 = new Button("Nine");
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);

f.setLayout(gl);
f.setSize(300, 300);
f.setVisible(true);
}
}
Output:
Card Layout:
package demo;
import java.awt.*;
import java.awt.event.*;
class CardLayoutExample extends Frame
{
CardLayout card = new CardLayout(90, 20);
CardLayoutExample()
{
setLayout(card);
setVisible(true);
setSize(200,200);
Button b1 = new Button("first ");
Button b2 = new Button("Second");
Button b3 = new Button("Third");
add(b1);
add(b2);
add(b3);
}
public static void main(String args[])
{
CardLayoutExample ce=new CardLayoutExample();
}

}
Output:
p

You might also like