0% found this document useful (0 votes)
33 views14 pages

Layout Managers

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

Layout Managers

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

Layout Managers

1. FlowLayout:
• Flow layout is the default Layout Manager.
• It works just like how text is arranged in a Text Edior. i.e.,
• The FlowLayout arrange the components in a line, one after another (in a flow).
import java.awt.*;
public class MyFlowLayout
{ MyFlowLayout()
{ Frame f=new Frame();
Button b1=new Button("1");
Button b2=new Button("2");
Button b3=new Button("3");
Button b4=new Button("4");
Button b5=new Button("5");
f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);
f.setLayout(new FlowLayout(FlowLayout.RIGHT));
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
MyFlowLayout myflow = new MyFlowLayout();
}
}
2. Border Layout:
• Border Layout is a layout manager that allows us to use different areas of a
screen.
• Border layout divides the screen into five parts out of which four are narrow in
corners and one large area in the center.
• Corners are termed as North, South, East, and West.
• The large area is called the center.
• Eg:
import java.awt.*;
public class Border
{
Border()
{
Frame f=new Frame();
Button b1=new Button("NORTH");;
Button b2=new Button("SOUTH");;
Button b3=new Button("EAST");;
Button b4=new Button("WEST");;
Button b5=new Button("CENTER");;

f.add(b1,BorderLayout.NORTH);
f.add(b2,BorderLayout.SOUTH);
f.add(b3,BorderLayout.EAST);
f.add(b4,BorderLayout.WEST);
f.add(b5,BorderLayout.CENTER);
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args)
{
Border br = new Border();
}
}
3. GridLayout:
• A grid is a two-dimensional structure in which components are aligned in rows
and columns.
• We need to understand that if we provide the number of columns as 0, the
length for the rows allowed will be infinite.
• Eg:
import java.awt.*;
public class MyGridLayout{
MyGridLayout(){
Frame f=new Frame();

Button b1=new Button("1");


Button b2=new Button("2");
Button b3=new Button("3");
Button b4=new Button("4");
Button b5=new Button("5");
Button b6=new Button("6");
Button b7=new Button("7");
Button b8=new Button("8");
Button b9=new Button("9");
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(new GridLayout(3,3));
//setting grid layout of 3 rows and 3 columns

f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new MyGridLayout();
}
}
Menu Bars and Menus
• The MenuBar class is used to display menu bar on the window or frame. It may
have several menus.
• An application can contain a MenuBar which in turn can contain one or more
Menu objects, which can contain one or more MenuItem objects, since Menu is
a subclass of MenuItem, each MenuItem can contain another Menu object as
well.
• Consider the example that shows how to create menu bar and menus..
import java.awt.*;
class MenuExample
{
public static void main(String args[])
{
Frame f= new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar(); // Creating Menu bar

Menu menu=new Menu("File"); //Creating File menu


Menu menu1=new Menu("Edit"); // Creating Edit menu

MenuItem i1=new MenuItem("New");


MenuItem i2=new MenuItem("Open");
MenuItem i3=new MenuItem("Cut");
MenuItem i4=new MenuItem("Copy");
menu.add(i1); menu.add(i2);
menu1.add(i3); menu1.add(i4);
mb.add(menu);
mb.add(menu1);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Dialogboxes
• Java class named JOptionPane that produce dialog boxes.
• A dialog box is a GUI object in which you can place messages that you want to
display on the screen.
• The showMessageDialog() method that is part of the JOptionPane class.
• The showMessageDialog() method is followed by a set of parentheses.
• The showMessageDialog() has two arguments.
• The first argument is null. It means the message box should be shown in the
center of the screen.
• After the comma, second argument is the string that should be output.
import javax.swing.JOptionPane;
public class DialogBoxinJavaExample
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null,"Dailog Box in Java Example");

}
}

You might also like