Showing posts with label layout manager in java. Show all posts
Showing posts with label layout manager in java. Show all posts

Wednesday, 12 June 2019

Layout Manger in java | Flow Layout in java - Online Help

Layout manager:

A layout manager is a special object that determines how the components of a container are organized. The layout manager is an instance of any class that implements the layout manager interface. The layout manager is set by the setLayout() method. The general format is:

void setLayout(LayoutManager obj)

Types of layout managers:

The various layout manager is as follows:
1.Flow Layout
2.Border Layout
3.Grid Layout
4.Card Layout

Flow Layout in java:

 Flow layout is the default layout manager for an applet. The FlowLayout class places the controls in a row that is centered in the available space. If the controls cannot fit in the current row, another row is started.

The FlowLayout class has three constructors. They are:

1.FlowLayout()
2.FlowLayout(int alignment)
3.FlowLayout(int alignment,int h,int v)

Parameter
Alignment-specifies how each line is aligned. The valid values are alignment. They are:
FlowLayout.LEFT
FlowLayout.RIGHT
FlowLayout.CENTER

h-specifies the horizontal or gap between controls.

v-specifies the vertical gap between controls.

Sample program:

import java.awt.*;
import java.aplet.*;
public class FlowLayoutDemo extends Applet
{
public void init()
{
setLayout(new FlowLayout(FlowLayout.RIGHT,5,5));
for(int i=1;i<12;i++)
{
add(new Button("toys"+i);
}
}
}


Grid layout in java - online help

Grid layout in java:

The Grid layout class automatically arranges components in a grid. The grid layout manager organizes the display into a rectangular grid. All components of the grid are created with equal size.

The grid layout class has three constructors. They are:
GridLayout()
GridLayout(int numrows,int numcols)
GridLayout(int numrows,int numcols,int h,int v)

Parameters:

numrows -determines the number of rows
numcols -determines the number of cols
 -specifies the horizontal gap in pixels between component.
v   -specifies the vertical gap in pixels between component.

Sample program grid layout in java:

import java.awt.*;
import java.applet.*;
public class GridLayoutDemo extends Applet
{
Button b1,b2,b3,b4;
GridLayout g=new GridLayout(2,2);
public void init()
{
setLayout(g);
b1= new Button("sehban");
b2= new Button("reem");
b3= new Button("arman");
b4= new Button("amaal");
add(b1);
add(b2);
add(b3);
add(b4);
}
}


Saturday, 18 May 2019

Border Layout in Java - Online Help

Border Layout in java:

The border layout class allows geographic terms: North, South, East, West, Center. The border Layout class has two constructors.namely,

BorderLayout()
It is used to create a default border layout.

BorderLayout(int h,int v)

It is used to create a border layout with the specified horizontal and vertical space between components. The border layout defined the following regions.
BorderLayout.NORTH
BorderLayout.SOUTH
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.CENTER

Sample program:

import java.awt.*;
import java.Applet.*;
public class BorderLayoutDemo extends Applet
{
public void init()
{
setLayout(new BorderLayout(5,5));
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");
add(b1,"North");
add(b2,"south");
add(b3,"east");
add(b4,"west");
add(b5,"center");
}
}

Card Layout in java - Online Help

Card Layout in java:

A card layout in java is a layout manager which treats each component as a  card. Only one card is visible at a time and the container acts as a stack of the card. The components are arranged like a deck of the card such that only one of them is visible at a time. In order to use card layout, it is required to create an object of type "panel" that will hold the cards. All this card must also be the objects of type "panel".The layout manager of the panel must be set to the card layout.

The card layout class in java defines the following constructors:

CardLayout()   //first

CardLayout(int hor,int ver)   //second

The first constructor creates a default card layout. The second constructor creates a card layout in which hor and ver specify the horizontal and vertical space left between each component respectively.

Sample program for card layout in java:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class cardley extends JFrame implements
ActionListener
{
Container c;
CardLayout card;
cardley()
{
c=getContentPane();
card=new CardLayout(50,50);
c.setLayout(card);
JButton b1=new JButton("first");
JButton b2=new JButton("second");
JButton b3=new JButton("third");
c.add("button",b1);
c.add("button",b2);
c.add("button",b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSEpublic void action performed(ActionEvent ae)
{
card.next(c);//when a button is clicked show the next card.
}
public static void main(String args[])
{
cardley ob=new cardley();
ob.setTitle("Card Layout...");
ob.setSize(600,250);
ob.setVisible(true);
}