Feedback Form

JFrame Layout Management: Adding Panels, Menus, and Content Panes

JFrame Layout Management: Adding Panels, Menus, and Content Panes

Introduction

जब भी हम Java Swing का use करके GUI Application बनाते हैं, तो JFrame उसका main container होता है। JFrame एक window होती है जिसमें हम buttons, text fields, panels, menus और अन्य UI components add करते हैं। लेकिन इन सबको सही तरीके से arrange करने के लिए हमें Layout Management समझना जरूरी है।

Layout Management का मतलब होता है — JFrame के अंदर components को logical और visually appealing तरीके से arrange करना। जैसे classroom में chairs rows में होती हैं, वैसे ही JFrame में elements को manage करने के लिए Layout Manager का use किया जाता है।

What is Layout Manager?

Layout Manager एक class होती है जो यह decide करती है कि JFrame या किसी container में components को कैसे arrange किया जाए। Layout Manager हर component की position और size को automatically handle करता है ताकि UI clean और responsive लगे।

Layout Manager के मुख्य प्रकार

  • FlowLayout – Components को left से right arrange करता है, जैसे text paragraphs।
  • BorderLayout – JFrame को 5 regions में divide करता है: North, South, East, West, Center।
  • GridLayout – Components को table (rows और columns) की तरह arrange करता है।
  • BoxLayout – Components को vertical या horizontal line में arrange करता है।
  • CardLayout – एक time पर एक panel दिखाता है, useful होता है tabs या form pages के लिए।
  • Null Layout – इसमें programmer खुद manually position और size set करता है।

FlowLayout Example

FlowLayout सबसे simple layout manager होता है। यह components को left से right arrange करता है और space खत्म होने पर अगली line में shift कर देता है।

import javax.swing.*; import java.awt.*; public class FlowLayoutExample { public static void main(String[] args) { JFrame frame = new JFrame("FlowLayout Example"); frame.setLayout(new FlowLayout()); frame.add(new JButton("Button 1")); frame.add(new JButton("Button 2")); frame.add(new JButton("Button 3")); frame.setSize(300, 200); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

ऊपर के example में तीन buttons horizontally align होंगे। जैसे-जैसे JFrame का size बदलेगा, buttons की position automatically adjust होगी।

BorderLayout Example

BorderLayout JFrame को 5 parts में divide करता है। हर region में एक component set किया जा सकता है।

import javax.swing.*; import java.awt.*; public class BorderLayoutExample { public static void main(String[] args) { JFrame frame = new JFrame("BorderLayout Example"); frame.setLayout(new BorderLayout()); frame.add(new JButton("North"), BorderLayout.NORTH); frame.add(new JButton("South"), BorderLayout.SOUTH); frame.add(new JButton("East"), BorderLayout.EAST); frame.add(new JButton("West"), BorderLayout.WEST); frame.add(new JButton("Center"), BorderLayout.CENTER); frame.setSize(400, 300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

इस Layout में प्रत्येक region अपने आप adjust होता है ताकि UI organized लगे।

GridLayout Example

GridLayout components को equal-sized grid cells में arrange करता है। यह एक table की तरह दिखता है।

import javax.swing.*; import java.awt.*; public class GridLayoutExample { public static void main(String[] args) { JFrame frame = new JFrame("GridLayout Example"); frame.setLayout(new GridLayout(2, 3)); for (int i = 1; i <= 6; i++) { frame.add(new JButton("Button " + i)); } frame.setSize(400, 200); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

ऊपर के program में 2 rows और 3 columns बनेंगे। हर cell में एक button equal size में होगा।

BoxLayout Example

BoxLayout का use तब किया जाता है जब हमें components को vertical या horizontal direction में line-up करना होता है।

import javax.swing.*; import java.awt.*; public class BoxLayoutExample { public static void main(String[] args) { JFrame frame = new JFrame("BoxLayout Example"); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(new JButton("Button 1")); panel.add(new JButton("Button 2")); panel.add(new JButton("Button 3")); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

यह layout vertical arrangement के लिए बहुत useful होता है, जैसे form design या setting menus।

Adding Panels to JFrame

JPanel एक lightweight container होता है जिसे हम JFrame में add करके अलग-अलग sections बना सकते हैं। हर panel का अपना layout हो सकता है।

import javax.swing.*; import java.awt.*; public class PanelExample { public static void main(String[] args) { JFrame frame = new JFrame("Panel Example"); frame.setLayout(new GridLayout(1, 2)); JPanel leftPanel = new JPanel(); leftPanel.setBackground(Color.CYAN); leftPanel.add(new JLabel("Left Panel")); JPanel rightPanel = new JPanel(); rightPanel.setBackground(Color.PINK); rightPanel.add(new JLabel("Right Panel")); frame.add(leftPanel); frame.add(rightPanel); frame.setSize(400, 200); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

इस तरह panels का use करके हम GUI को logically divide कर सकते हैं और हर section को अलग layout दे सकते हैं।

Adding Menus to JFrame

Menu bar application का integral part होता है, जैसे “File”, “Edit”, “Help” menus। Java Swing में menus को JMenuBar, JMenu और JMenuItem classes से बनाया जाता है।

import javax.swing.*; public class MenuExample { public static void main(String[] args) { JFrame frame = new JFrame("Menu Example"); JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); JMenu editMenu = new JMenu("Edit"); JMenuItem newItem = new JMenuItem("New"); JMenuItem saveItem = new JMenuItem("Save"); JMenuItem exitItem = new JMenuItem("Exit"); fileMenu.add(newItem); fileMenu.add(saveItem); fileMenu.add(exitItem); menuBar.add(fileMenu); menuBar.add(editMenu); frame.setJMenuBar(menuBar); frame.setSize(400, 300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

ऊपर के code में JFrame में एक menu bar add की गई है जिसमें “File” और “Edit” menus हैं। “File” में तीन menu items दिए गए हैं।

Understanding Content Pane in JFrame

हर JFrame के अंदर एक Content Pane होता है जो actually components को hold करता है। जब हम frame.add() करते हैं, तो internally component content pane में add होता है।

अगर हमें multiple panels या special layouts manage करने हैं, तो हम getContentPane() का use कर सकते हैं।

import javax.swing.*; import java.awt.*; public class ContentPaneExample { public static void main(String[] args) { JFrame frame = new JFrame("Content Pane Example"); Container contentPane = frame.getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(new JButton("OK")); contentPane.add(new JButton("Cancel")); frame.setSize(300, 200); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

यहाँ हमने Content Pane को access करके उस पर directly buttons add किए हैं। यह approach advanced GUI designs में useful होती है।

Layout Manager Comparison Table

Layout Type Arrangement Style Best Use Case
FlowLayout Left to Right Simple forms, small UI
BorderLayout Five Regions Main windows, dashboards
GridLayout Equal cells Calculator, tables
BoxLayout Vertical/Horizontal line Menus, lists, forms
CardLayout Stacked panels Tabbed panes, wizards

Exam-Oriented Notes

  • JFrame Swing का top-level container होता है।
  • Layout Manager components की positioning और sizing control करता है।
  • FlowLayout – left to right arrangement करता है।
  • BorderLayout – JFrame को 5 regions में divide करता है।
  • GridLayout – equal-sized grid बनाता है।
  • BoxLayout – vertical या horizontal line में components रखता है।
  • JPanel – lightweight container है जिसमें हम multiple components या layouts रख सकते हैं।
  • JMenuBar, JMenu और JMenuItem menus बनाने के लिए use होते हैं।
  • Content Pane JFrame का actual area है जहाँ components add होते हैं।
  • CardLayout का use multi-screen UI या tabs के लिए किया जाता है।
  • Exam में short answer के लिए याद रखें — “FlowLayout” default layout होता है JPanel का।