গ্রীড লেয়াঊট

GridLayout Arranges the components into rows and columns.
গ্রীড লেয়াঊট ম্যানেজার container কে গ্রীড এ ভাগ করে থাকে তার ফলে কম্পনেন্ট রো ও কলাম এ বিভক্ত হয়।প্রতিটি row and column এর একই width ও height থাকে। row এবং column গুলো বাম পার্শের উপড় থেকে ডান পার্শে বিস্তৃত হতে থাকে । এবং এভাবে চলতে থাকে শেষ পর্যন্ত।

একটি প্রোগ্রামের মাধ্যমে দেখা যাক-
import java.awt.GridLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;

public class GridLayoutFrame extends JFrame implements ActionListener
{
private JButton[] buttons;   private static final String[] names =
{ “one”, “two”, “three”, “four”, “five”, “six” };
private boolean toggle = true;
private Container container;
private GridLayout gridLayout1;
private GridLayout gridLayout2;
public GridLayoutFrame()
{
super( “GridLayout Demo” );
gridLayout1 = new GridLayout( 2, 3, 5, 5 );
gridLayout2 = new GridLayout( 3, 2 );
container = getContentPane();
setLayout( gridLayout1 );
buttons = new JButton[ names.length ];
for ( int count = 0; count < names.length; count++ )
{
buttons[ count ] = new JButton( names[ count ] );
buttons[ count ].addActionListener( this );
add( buttons[ count ] );
}
}
public void actionPerformed( ActionEvent event )
{
if ( toggle )
container.setLayout( gridLayout2 );
else
container.setLayout( gridLayout1 );

toggle = !toggle;
container.validate();
}
}

import javax.swing.JFrame;

public class GridLayoutDemo
{
public static void main( String[] args )
{
GridLayoutFrame gridLayoutFrame = new GridLayoutFrame();
gridLayoutFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
gridLayoutFrame.setSize( 300, 200 );
gridLayoutFrame.setVisible( true );
}
}

কোডের ব্যাক্ষা———–

import java.awt.GridLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;

public class GridLayoutFrame extends JFrame implements ActionListener
{
private JButton[] buttons; // array of buttons
private static final String[] names =
{ “one”, “two”, “three”, “four”, “five”, “six” };
private boolean toggle = true; // toggle between two layouts
private Container container; // frame container
private GridLayout gridLayout1; // first gridlayout
private GridLayout gridLayout2; // second gridlayout

// no-argument constructor
public GridLayoutFrame()
{
super( “GridLayout Demo” );
gridLayout1 = new GridLayout( 2, 3, 5, 5 ); // 2 by 3; gaps of 5
gridLayout2 = new GridLayout( 3, 2 ); // 3 by 2; no gaps
container = getContentPane(); // get content pane
setLayout( gridLayout1 ); // set JFrame layout
buttons = new JButton[ names.length ]; // create array of JButtons

for ( int count = 0; count < names.length; count++ )
{
buttons[ count ] = new JButton( names[ count ] );
buttons[ count ].addActionListener( this ); // register listener
add( buttons[ count ] ); // add button to JFrame
} // end for
} // end GridLayoutFrame constructor

// handle button events by toggling between layouts
public void actionPerformed( ActionEvent event )
{
if ( toggle )
container.setLayout( gridLayout2 ); // set layout to second
else
container.setLayout( gridLayout1 ); // set layout to first

toggle = !toggle; // set toggle to opposite value
container.validate(); // re-lay out container
} // end method actionPerformed
} // end class GridLayoutFrame

import javax.swing.JFrame;

public class GridLayoutDemo
{
public static void main( String[] args )
{
GridLayoutFrame gridLayoutFrame = new GridLayoutFrame();
gridLayoutFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
gridLayoutFrame.setSize( 300, 200 ); // set frame size
gridLayoutFrame.setVisible( true ); // display frame
} // end main
} // end class GridLayoutDemo

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s