iTechtalk

Quick Search

Go Advanced

Member Login

Not registered? | Forgot Password
 
Register
Welcome
 
iTechtalk > Tutorials > Programming » The GridLayout Class Used in AWT
Reply
Old 10-07-2008, 11:30 AM   #1 (permalink)
 
Freelancer's Avatar
 
Senior Member
Join Date: Oct 2008
Posts: 907
Post The GridLayout Class Used in AWT

The GridLayout Class Used in AWT



The GridLayout class represents a second type of layout manager. A GridLayout puts components within a grid with a specified number of rows and columns. All components within the grid are of equal size.

Creating a GridLayout is similar to creating a FlowLayout, except that it has different constructor methods. The default constructor creates a layout including a single component per column. The other two are listed below:

GridLayout (int rows, int cols);
GridLayout (int rows, int cols, int hgap, int vgap);


Use of these constructors should be self-explanatory. If you don’t specify the gaps their values will default to zero. If you give either the rows or columns value as zero, or you add more than rows x columns components, the layout manager will adjust its values so that the components still fit within the grid.

The following listing, GridTest, creates a 3 x 3 arrangement of labels. There is a five-pixel horizontal and vertical gap between the labels. Each label background color is green so you can see the gaps.

import java.applet.*;
import java.awt.*;
public class GridTest extends Applet
{
public void init()
{
// create a String and a StringTokenizer to parse the String
String string = "My Head Is My Only House Unless It Rains";
java.util.StringTokenizer st = new java.util.StringTokenizer(string);
// create a 3 by 3 grid layout with a 5 pixel gap between components
setLayout(new GridLayout(3, 3, 5, 5));
// for each String token, create a label with a green background and
// add it to the panel
while(st.hasMoreTokens())
{
Label label = new Label(st.nextToken(), Label.CENTER);
label.setBackground(Color.green);
add(label);
}
}
} // GridTest
Freelancer is offline   Reply With Quote
 
Reply

Bookmarks

Tags
awt, gridlayout class in awt, java, java awt

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using the validation class in PHP tech4ever Programming 0 11-05-2008 09:59 AM
The ActorGroup2D Class crazy Programming 0 10-10-2008 11:28 AM
The ImageLoader Class crazy Programming 0 10-10-2008 11:25 AM
Class Drivers traffic Operating systems 0 10-10-2008 11:19 AM
The GradientPaint Class genius Programming 0 10-08-2008 09:50 AM


 

Content Relevant URLs by vBSEO 3.3.0