RKTechGame | Java Swing Layouts - Detailed Guide

Java Swing Layout Managers kya hain?

Layout managers wo classes hoti hain jo Swing components ko container me arrange karti hain based on alag algorithms. Ye components ki positioning, sizing aur alignment control karti hain taaki GUI flexible aur user-friendly lage.

Swing me default layout managers provide kiye gaye hain, aur hum apni jarurat ke mutabik inhe set kar sakte hain:

Common Swing Layout Managers

Layout Manager Description Use Case
FlowLayout Components ko left se right flow karata hai, wrapping ke sath. Simple panels jahan components horizontal line me lagen.
BorderLayout Container ko North, South, East, West, Center me divide karta hai. Frame ke main parts jaise header, footer, sidebar layout.
GridLayout Equal size ke grid cells me components ko arrange karta hai. Calculator buttons, badi forms.
BoxLayout Components ko vertically ya horizontally stack karta hai. Vertical menus, toolbar.
CardLayout Ek container me multiple panels rakhta hai, ek samay me ek dikhata hai. Wizard, tabbed interface.
GridBagLayout Sabse flexible, grid me components ko spanning aur customizable sizing ke saath arrange karta hai. Complex forms, interactive GUIs.

FlowLayout Example

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(FlowLayout.CENTER, 15, 10));

    for(int i = 1; i <= 4; i++) {
      frame.add(new JButton("Button " + i));
    }

    frame.setSize(350, 150);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

BorderLayout Example

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(10, 10));

    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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

GridLayout Example

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, 5, 5)); // 2 rows, 3 cols

    for(int i = 1; i <= 6; i++) {
      frame.add(new JButton("Button " + i));
    }

    frame.setSize(400, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

BoxLayout Example

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

public class BoxLayoutExample {
  public static void main(String[] args) {
    JFrame frame = new JFrame("BoxLayout Example");
    frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));

    for(int i = 1; i <= 5; i++) {
      JButton button = new JButton("Button " + i);
      button.setAlignmentX(Component.CENTER_ALIGNMENT);
      frame.add(button);
    }

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

CardLayout Example

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

public class CardLayoutExample {
  public static void main(String[] args) {
    JFrame frame = new JFrame("CardLayout Example");
    CardLayout cardLayout = new CardLayout();
    frame.setLayout(cardLayout);

    JPanel panel1 = new JPanel();
    panel1.add(new JLabel("Card 1"));

    JPanel panel2 = new JPanel();
    panel2.add(new JLabel("Card 2"));

    frame.add(panel1, "Card1");
    frame.add(panel2, "Card2");

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

    new Timer(3000, e -> cardLayout.show(frame.getContentPane(), "Card2")).start();
  }
}

GridBagLayout Introduction

GridBagLayout sabse flexible layout hai jo grid-based hota hai. Isme har component ko grid cell ke specific position, size, aur spanning control kar sakte hain. Har component ke saath GridBagConstraints use karke layout customize karte hain.

GridBagLayout ka example thoda complex hota hai, isliye aage ke advanced articles me ye cover karenge.