Feedback Form

Layout Managers for AWT: FlowLayout, BorderLayout, GridLayout

Layout Managers for AWT: FlowLayout, BorderLayout, GridLayout

Layout Managers in AWT क्या होते हैं?

जब हम Java AWT (Abstract Window Toolkit) में GUI applications बनाते हैं, तो हमें अलग-अलग components जैसे Button, Label, TextField, Checkbox आदि को किसी window या frame पर सही ढंग से arrange करना पड़ता है। यही काम Layout Manager करता है। यह तय करता है कि components screen पर कैसे दिखेंगे, उनका size क्या होगा, और वे एक-दूसरे के साथ कैसे align होंगे। सीधे शब्दों में कहें, Layout Manager हमारे GUI design को systematic और responsive बनाता है।

अगर हम manually component की position set करें (setBounds() method से), तो हर screen size पर layout बिगड़ सकता है। लेकिन Layout Managers automatic arrangement provide करते हैं जिससे design हर resolution पर अच्छा दिखे।

AWT में मुख्य Layout Managers

Java AWT में कई तरह के Layout Managers available हैं, लेकिन यहाँ हम तीन सबसे महत्वपूर्ण layout managers को detail में समझेंगे:

  • FlowLayout
  • BorderLayout
  • GridLayout

FlowLayout in AWT

FlowLayout सबसे simple और commonly used layout manager है। यह components को left से right arrange करता है — जैसे हम English text पढ़ते हैं। जब एक row भर जाती है, तो अगला component नई line में चला जाता है।

FlowLayout की Default Settings

  • Alignment: Center (components बीच में आते हैं)
  • Horizontal gap: 5 pixels (default)
  • Vertical gap: 5 pixels (default)

FlowLayout Constructor

FlowLayout() FlowLayout(int align) FlowLayout(int align, int hgap, int vgap)

Example Code (FlowLayout)

import java.awt.*; import java.awt.event.*; public class FlowLayoutExample { public static void main(String[] args) { Frame f = new Frame("FlowLayout Example"); f.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20)); Button b1 = new Button("Button 1"); Button b2 = new Button("Button 2"); Button b3 = new Button("Button 3"); Button b4 = new Button("Button 4"); f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.setSize(300, 200); f.setVisible(true); } }

FlowLayout के फायदे

  • Simple और easy-to-use layout है।
  • Automatically adjust हो जाता है जब window resize होती है।
  • Small applications के लिए perfect layout है।

FlowLayout की सीमाएँ

  • Complex GUI में proper alignment control मुश्किल होता है।
  • Large number of components होने पर layout messy लग सकता है।

BorderLayout in AWT

BorderLayout थोड़ा advanced layout manager है जो container को पाँच हिस्सों में divide करता है — North, South, East, West और Center। हर component को इन specific areas में add किया जाता है।

BorderLayout Constructor

BorderLayout() BorderLayout(int hgap, int vgap)

BorderLayout के Regions

Region Description
North Top area (usually header या title bar के लिए)
South Bottom area (status bar या footer)
East Right side area
West Left side area
Center Main content area (largest section)

Example Code (BorderLayout)

import java.awt.*; public class BorderLayoutExample { public static void main(String[] args) { Frame f = new Frame("BorderLayout Example"); f.setLayout(new BorderLayout(10, 10)); 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(400, 250); f.setVisible(true); } }

BorderLayout के फायदे

  • GUI को logically divide करने के लिए best layout है।
  • Responsive design automatically adjust होता है।
  • Every region में एक specific component assign किया जा सकता है।

BorderLayout की सीमाएँ

  • एक region में एक ही component add किया जा सकता है।
  • Layout design थोड़ा rigid हो जाता है।

GridLayout in AWT

GridLayout का काम है container को equal-sized cells (rows × columns) में divide करना। हर cell में एक component आता है और सभी cells का size equal होता है। यह layout calculator या form जैसे design में बहुत useful है।

GridLayout Constructor

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

Example Code (GridLayout)

import java.awt.*; public class GridLayoutExample { public static void main(String[] args) { Frame f = new Frame("GridLayout Example"); f.setLayout(new GridLayout(2, 3, 10, 10)); for(int i = 1; i <= 6; i++) { f.add(new Button("Button " + i)); } f.setSize(300, 200); f.setVisible(true); } }

GridLayout के फायदे

  • All components equal size में दिखते हैं।
  • Clean और structured design के लिए perfect है।
  • Calculator या table-style UI में useful होता है।

GridLayout की सीमाएँ

  • Component size को individually control नहीं किया जा सकता।
  • Complex layouts के लिए flexibility कम है।

तीनों Layout Managers की Comparison Table

Feature FlowLayout BorderLayout GridLayout
Arrangement Left to Right, then next line 5 Regions (N, S, E, W, Center) Grid of Rows × Columns
Flexibility High Medium Low
Equal Component Size No No Yes
Best Used For Simple UI Logical Area Division Form/Calculator Layouts

Layout Manager कैसे Set करें?

हर AWT container जैसे Frame या Panel में default layout manager set होता है। उसे change करने के लिए हम setLayout() method का use करते हैं। उदाहरण:

Frame f = new Frame(); f.setLayout(new FlowLayout());

Layout Managers को Combine कैसे करें?

हम अलग-अलग panels बनाकर उनमें different layout managers लगा सकते हैं। जैसे main frame में BorderLayout हो और अंदर के panels में FlowLayout या GridLayout। इससे UI बहुत flexible और professional दिखता है।

Panel p1 = new Panel(); p1.setLayout(new FlowLayout()); Panel p2 = new Panel(); p2.setLayout(new GridLayout(2,2));

Exam Point of View – Important Notes

  • FlowLayout — Default layout manager for Panel।
  • BorderLayout — Default layout manager for Frame, Window, Dialog।
  • Layout manager automatically component को arrange करता है।
  • setLayout(null) करने पर manual positioning करनी पड़ती है।
  • हर layout का use case अलग होता है — use as per requirement।

Real Life Usage

Practical development में हम अक्सर combination layout use करते हैं। Example के लिए:

  • Top area में BorderLayout (North)
  • Buttons के लिए FlowLayout (Bottom)
  • Form fields के लिए GridLayout (Center)

Key Takeaways

  • FlowLayout — Simple linear arrangement।
  • BorderLayout — Logical area-based arrangement।
  • GridLayout — Uniform size components के लिए।
  • हर layout का अपना importance है, choose wisely।