|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis article will demonstrate how to use the 'Grid' Layout in Java. The Grid Layout Managers assigns components to a grid of cells. Each cell in the grid is the same size and the components grow to fill the available area. This Layout Manager is good for laying out containers that look like grids; e.g., a calculator, a calendar page or a battleship game.
The following constructors apply to the Grid Layout: // Imports import java.awt.*; import java.applet.Applet; public class Grid extends Applet{ // Adding Labels Label one = new Label("Team Name"); Label two = new Label("Stadium"); Label three = new Label("Nick Name"); Label four = new Label("Grimsby Town"); Label five = new Label("Blundell Park"); Label six = new Label("Mariners"); Label seven = new Label("Kettering Town"); Label eight = new Label("Rockignham Road"); Label nine = new Label("Poppies"); Label ten = new Label("Boston United"); Label eleven= new Label("York Street"); Label twelve= new Label("Pilgrims"); //The Grid Layout uses the simplest form of the add method which requires only a reference to a component. public void init(){ setLayout(new GridLayout(4,3)); add(one); one.setBackground(Color.red); add(two); two.setBackground(Color.red); add(three); three.setBackground(Color.red); add(four); four.setBackground(Color.green); add(five); five.setBackground(Color.green); add(six); six.setBackground(Color.green); add(seven); seven.setBackground(Color.blue); add(eight); eight.setBackground(Color.blue); add(nine); nine.setBackground(Color.blue); add(ten); ten.setBackground(Color.orange); add(eleven); eleven.setBackground(Color.orange); add(twelve); twelve.setBackground(Color.orange); } }
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||