Feedback Form

JFrame in Java Swing: Complete Guide to Window Creation (2025 Update)

JFrame in Java Swing: Complete Guide to Window Creation (2025 Update)

JFrame क्या है?

Java Swing में JFrame एक main container class होती है जिसका उपयोग GUI (Graphical User Interface) बनाने के लिए किया जाता है। इसे हम एक window की तरह use करते हैं जहाँ button, label, text field जैसे components add किए जाते हैं। जब आप कोई desktop application बनाते हैं, तो JFrame उस application का main window होता है।

Simple शब्दों में कहें तो JFrame वो box है जिसमें आपके सारे GUI elements दिखाई देते हैं।

JFrame का use क्यों किया जाता है?

Java Swing में JFrame का use window या frame create करने के लिए किया जाता है। JFrame हमें user interface को design करने की सुविधा देता है और साथ ही user से interaction (जैसे button click या text input) लेने में मदद करता है।

  • JFrame से आप GUI based desktop application बना सकते हैं।
  • यह event handling को support करता है।
  • आप इसमें multiple components add कर सकते हैं।
  • यह AWT के मुकाबले lightweight और fast होता है।

JFrame का Syntax

JFrame को use करने के लिए पहले आपको javax.swing.JFrame package import करना होता है।

import javax.swing.*;

public class MyFrame {
  public static void main(String args[]) {
    JFrame frame = new JFrame("My First Frame");
    frame.setSize(400,300);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

ऊपर दिए गए example में:

  • setSize(400,300): Frame की width और height set करता है।
  • setVisible(true): Frame को screen पर visible बनाता है।
  • setDefaultCloseOperation: Frame बंद करने पर program terminate करता है।

JFrame के Important Methods

JFrame में कई useful methods होते हैं जो window को customize करने में मदद करते हैं। नीचे कुछ commonly used methods दिए गए हैं:

MethodDescription
setTitle(String title)Frame का title set करता है।
setSize(int w, int h)Frame की चौड़ाई और ऊँचाई निर्धारित करता है।
setVisible(boolean b)Frame को दिखाता या छुपाता है।
setLocation(int x, int y)Frame को screen पर specific position पर रखता है।
setLayout(LayoutManager l)Layout manager सेट करता है जैसे FlowLayout, BorderLayout आदि।
setDefaultCloseOperation(int op)Close बटन की functionality तय करता है।
add(Component c)Frame में कोई component (जैसे button, label) add करता है।

Simple JFrame Example

नीचे एक simple JFrame example दिया गया है जो Java Swing में एक basic window create करता है:

import javax.swing.*;

public class SimpleFrame {
  public static void main(String[] args) {
    JFrame f = new JFrame("Simple JFrame Example");
    f.setSize(400, 300);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

जब आप इसे run करते हैं तो एक empty window दिखाई देगी जिसके ऊपर "Simple JFrame Example" title होगा।

JFrame में Components जोड़ना

JFrame में आप कई Swing components add कर सकते हैं जैसे:

  • JLabel – Text दिखाने के लिए
  • JButton – Button create करने के लिए
  • JTextField – User से input लेने के लिए

Example:

import javax.swing.*;

public class AddComponents {
  public static void main(String[] args) {
    JFrame f = new JFrame("Add Components Example");
    JLabel label = new JLabel("Welcome to JFrame!");
    JButton btn = new JButton("Click Me");

    label.setBounds(50, 50, 200, 30);
    btn.setBounds(80, 100, 100, 30);

    f.add(label);
    f.add(btn);

    f.setSize(300, 200);
    f.setLayout(null);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

ऊपर के example में हमने absolute positioning का use किया है, इसलिए setLayout(null) लगाया गया है।

JFrame में Layout Managers

Layout managers JFrame में components को organize करने के लिए use होते हैं। ये automatically decide करते हैं कि component कहाँ और कैसे दिखेगा।

  • FlowLayout: Components को row में arrange करता है।
  • BorderLayout: Components को North, South, East, West और Center में arrange करता है।
  • GridLayout: Components को grid (rows & columns) में arrange करता है।

Example (FlowLayout):

import javax.swing.*;
import java.awt.*;

public class LayoutExample {
  public static void main(String[] args) {
    JFrame f = new JFrame("FlowLayout Example");
    f.setLayout(new FlowLayout());
    f.add(new JButton("Button 1"));
    f.add(new JButton("Button 2"));
    f.add(new JButton("Button 3"));
    f.setSize(300,150);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

JFrame में Event Handling

Event handling का मतलब होता है user की action को capture करना जैसे button click, mouse move, key press आदि। JFrame में event handling listener interfaces के through की जाती है।

Example:

import javax.swing.*;
import java.awt.event.*;

public class EventDemo extends JFrame implements ActionListener {
  JButton btn;

  EventDemo() {
    btn = new JButton("Click Me");
    btn.setBounds(100,100,100,30);
    btn.addActionListener(this);

    add(btn);
    setLayout(null);
    setSize(300,200);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public void actionPerformed(ActionEvent e) {
    btn.setText("Clicked!");
  }

  public static void main(String[] args) {
    new EventDemo();
  }
}

यहाँ जब आप button पर click करते हैं तो उसका text “Clicked!” में बदल जाता है।

AWT और Swing में अंतर

AWTSwing
Heavyweight components होते हैं।Lightweight components होते हैं।
Platform dependent होता है।Platform independent होता है।
Limited look and feel support करता है।Multiple look and feel support करता है।
Older technology है।Modern GUI toolkit है।

Exam Point of View से JFrame

College exams में JFrame पर short answer और coding based questions बहुत common होते हैं। कुछ frequently पूछे जाने वाले topics हैं:

  • JFrame क्या है और इसका उपयोग बताइए।
  • JFrame का syntax और constructor लिखिए।
  • JFrame में components add करने का तरीका बताइए।
  • JFrame में layout managers का उपयोग क्यों किया जाता है?
  • Event handling का example दीजिए।

Important Points to Remember

  • हर JFrame object एक separate window represent करता है।
  • JFrame को visible करने के लिए हमेशा setVisible(true) लगाएँ।
  • Program बंद करने के लिए setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) जरूरी है।
  • Layout managers automatically component की positioning handle करते हैं।
  • Swing lightweight है और platform independent होता है।

Quick Notes for Revision

  • Class: javax.swing.JFrame
  • Superclass: java.awt.Frame
  • Package: javax.swing
  • Purpose: GUI window create करने के लिए
  • Common Methods: setTitle(), setSize(), add(), setVisible(), setLayout()
  • Default Layout: BorderLayout

Real Life Example

अगर आप Notepad, Calculator या कोई simple form application बनाना चाहते हैं, तो JFrame उसकी main window की तरह काम करता है। इसके अंदर आप buttons, textfields, menus आदि add करके interactive application बना सकते हैं।

Summary (Exam-Ready Notes)

  • JFrame Java Swing का सबसे basic container है।
  • यह graphical window create करने के लिए use होता है।
  • इसमें हम buttons, labels, textfields add कर सकते हैं।
  • setSize(), setVisible(), setLayout() जैसे methods frequently use होते हैं।
  • Layout Managers components की positioning को manage करते हैं।
  • Event handling के लिए listeners का use किया जाता है।