Java Swing – Introduction जावा स्विंग परिचय, Swing Core Components स्विंग के मुख्य अवयव

Swing, जावा का Modern GUI (Graphical User Interface) Toolkit है, जो प्लेटफॉर्म-इंडिपेंडेंट Desktop Applications बनाने के लिए प्रयोग किया जाता है। Swing is Java’s modern GUI toolkit used to create platform-independent desktop applications.


🖥️ Swing की विशेषताएँ / Features of Swing

  1. Lightweight Components
    Swing के सभी GUI components lightweight होते हैं और प्लेटफॉर्म पर निर्भर नहीं होते।
    All Swing components are lightweight and platform-independent.

  2. Rich Set of Components
    Swing में JButton, JTextField, JCheckBox, JTable, JMenu, JProgressBar आदि जैसे बहुत से advanced GUI components मौजूद हैं।
    Swing provides advanced GUI components like JButton, JTextField, JCheckBox, JTable, JMenu, JProgressBar, etc.

  3. Pluggable Look and Feel
    Swing एप्लिकेशन का look and feel change किया जा सकता है, जैसे Windows, Metal या Nimbus।
    Swing applications support changing their look and feel, e.g., Windows, Metal, Nimbus.

  4. MVC Architecture
    Swing components Model-View-Controller (MVC) architecture पर काम करते हैं।
    Swing components follow the Model-View-Controller (MVC) architecture.

  5. Event-Driven Programming
    Swing इवेंट ड्रिवेन होती है, यानी सभी actions जैसे button click, mouse hover, key press events से handle होते हैं।
    Swing is event-driven, meaning actions like button clicks, mouse hovers, and key presses are handled via events.


🏗️ Swing के Common Classes

  1. JFrame → Main Window बनाने के लिए।
    JFrame → Used to create the main window.

  2. JPanel → GUI components को रखने के लिए।
    JPanel → Used as a container for GUI components.

  3. JButton → Button create करने के लिए।
    JButton → Used to create buttons.

  4. JLabel → Text या Image display करने के लिए।
    JLabel → Used to display text or images.

  5. JTextField → Single line text input के लिए।
    JTextField → Used for single-line text input.


💻 पहला Swing Program – Basic JFrame

import javax.swing.*;
class FirstSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("My First Swing Example");
frame.setSize(400, 300); // width, height
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Output:

  • एक खाली 400x300 JFrame Window दिखाई देगी।


Key Points:

  • Swing classes javax.swing package में होती हैं।

  • JFrame पर components जोड़ने के लिए add() method का use करते हैं।

  • एप्लिकेशन को बंद करने के लिए setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) जरूरी है। 


🌟 Java Swing Core Components


1️⃣ JFrame – Main Window

JFrame is the main window in any Swing application. It acts as a container that holds all GUI components like buttons, labels, and text fields.

JFrame, किसी भी Swing एप्लिकेशन की मुख्य विंडो होती है। यह एक कंटेनर की तरह काम करती है, जिसमें बटन, लेबल और टेक्स्ट फील्ड जैसे सभी GUI components रखे जाते हैं।

Key Points:

  • JFrame is top-level container.

  • Use setSize(width, height) to define its size.

  • Use setVisible(true) to make it visible.

  • Close operation must be set with setDefaultCloseOperation().

Example:

import javax.swing.*;

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

2️⃣ JPanel – Sub Container

JPanel is a sub-container used inside a JFrame to organize GUI components. It helps in grouping elements and managing layouts.

JPanel, JFrame के अंदर प्रयोग होने वाला सब-कंटेनर है। इसका उपयोग elements को group करने और layout manage करने के लिए किया जाता है।

Key Points:

  • JPanel is invisible by default.

  • You can add multiple JPanels in a JFrame.

  • It supports layouts like FlowLayout, BorderLayout, GridLayout.

Example:

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

class JPanelExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JPanel Example");
        JPanel panel = new JPanel(); // create panel

        panel.add(new JButton("Click Me")); // add button in panel

        frame.add(panel);  // add panel to frame
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

3️⃣ JButton – Button Component

JButton is a clickable button that performs an action when clicked.

JButton एक क्लिक करने योग्य बटन है, जिस पर क्लिक करने पर कोई कार्य (action) perform होता है।

Key Points:

  • Use new JButton("Text") to create a button.

  • Events are handled using ActionListener.

Example:

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

class JButtonExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JButton Example");
        JButton button = new JButton("Click Me");

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Button Clicked!");
            }
        });

        frame.add(button);
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

4️⃣ JLabel – Display Text or Image

JLabel is used to display static text, image, or both in a Swing window.

JLabel का प्रयोग स्थिर टेक्स्ट, इमेज या दोनों को Swing विंडो में प्रदर्शित करने के लिए किया जाता है।

Key Points:

  • JLabel cannot take input; it is read-only.

  • Supports image icons with text.

Example:

import javax.swing.*;

class JLabelExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JLabel Example");
        JLabel label = new JLabel("Welcome to Java Swing!");
        
        frame.add(label);
        frame.setSize(350, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

5️⃣ JTextField – Single Line Input Field

JTextField is used to take single-line input from the user in a Swing application.

JTextField का उपयोग यूज़र से एक लाइन का इनपुट लेने के लिए किया जाता है।

Key Points:

  • Use new JTextField(columns) to create text field.

  • Use getText() to read input and setText() to set text.

Example:

import javax.swing.*;

class JTextFieldExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JTextField Example");
        JTextField textField = new JTextField(20);
        
        frame.add(textField);
        frame.setSize(350, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

🎯 Summary

Component Purpose / उद्देश्य
JFrame Main Window (मुख्य विंडो)
JPanel Sub-container for GUI components
JButton Clickable Button (क्लिक करने वाला बटन)
JLabel Display text or image
JTextField Take single-line input from user


Example:- Java Swing Stylish Login Form with Background Image

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

class StylishLoginForm extends JFrame {
    StylishLoginForm() {
        // Frame Settings
        setTitle("Stylish Login Form");
        setSize(500, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(null);

        // Background Image
        JLabel background = new JLabel(new ImageIcon("background.jpg"));
        background.setBounds(0, 0, 500, 300);
        setContentPane(background);
        setLayout(null);

        // Font for Labels and TextFields
        Font font = new Font("Arial", Font.BOLD, 16);

        // Username Label
        JLabel userLabel = new JLabel("Username:");
        userLabel.setBounds(80, 70, 100, 30);
        userLabel.setForeground(Color.WHITE);
        userLabel.setFont(font);
        add(userLabel);

        // Username Field
        JTextField userText = new JTextField();
        userText.setBounds(200, 70, 200, 30);
        userText.setFont(font);
        add(userText);

        // Password Label
        JLabel passLabel = new JLabel("Password:");
        passLabel.setBounds(80, 120, 100, 30);
        passLabel.setForeground(Color.WHITE);
        passLabel.setFont(font);
        add(passLabel);

        // Password Field
        JPasswordField passText = new JPasswordField();
        passText.setBounds(200, 120, 200, 30);
        passText.setFont(font);
        add(passText);

        // Login Button
        JButton loginButton = new JButton("Login");
        loginButton.setBounds(200, 180, 100, 35);
        loginButton.setBackground(new Color(30, 144, 255));
        loginButton.setForeground(Color.WHITE);
        loginButton.setFont(new Font("Arial", Font.BOLD, 16));
        add(loginButton);

        // Button Action
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String username = userText.getText();
                String password = new String(passText.getPassword());

                if (username.equals("admin") && password.equals("1234")) {
                    JOptionPane.showMessageDialog(null, 
                        "Login Successful! Welcome " + username);
                } else {
                    JOptionPane.showMessageDialog(null, 
                        "Invalid Username or Password!",
                        "Login Error",
                        JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        setVisible(true);
    }

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

📝 How It Works / यह कैसे काम करता है

  1. Background image is applied using a JLabel with ImageIcon.

  2. Custom font & colors are used to make it attractive.

  3. Button color is changed using setBackground() and setForeground().

  4. JOptionPane shows messages on login success or failure.

🎨 Output Look

+---------------------------------------+
|     [ Background Image Visible ]       |
|  Username: [__________]                |
|  Password: [__________]                |
|               [ Login ]                |
+---------------------------------------+

Comments

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध