Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

In Java, particularly in the context of GUI (Graphical User Interface) development using Swing and AWT (Abstract Window Toolkit), containers, components, and layout managers are key concepts. Here’s a detailed description of each with examples:

1. Containers

Containers are special components in Java that can hold and organize other components (like buttons, text fields, labels, etc.). Common containers include JFrame, JPanel, JDialog, and JApplet.

Example: JFrame and JPanel

				
					import javax.swing.*;

public class ContainerExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Container Example");
        JPanel panel = new JPanel();
        
        JButton button1 = new JButton("Button 1");
        JButton button2 = new JButton("Button 2");
        
        panel.add(button1);
        panel.add(button2);
        
        frame.add(panel);
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

				
			
  • JFrame is a top-level container that represents the main window.
  • JPanel is an intermediate container that can hold multiple components.

2. Components

Components are the basic building blocks of a GUI application in Java. They include elements like buttons, labels, text fields, etc.

Example: Adding Components to a Panel

				
					import javax.swing.*;

public class ComponentExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Component Example");
        JPanel panel = new JPanel();
        
        JLabel label = new JLabel("Label:");
        JTextField textField = new JTextField(10);
        JButton button = new JButton("Submit");
        
        panel.add(label);
        panel.add(textField);
        panel.add(button);
        
        frame.add(panel);
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

				
			
  • JLabel is a component that displays a text label.
  • JTextField is a component that allows for single-line text input.
  • JButton is a component that triggers an action when clicked.

3. Layout Managers

Layout Managers are used to arrange components within a container. Different layout managers provide different ways of organizing components.

Common Layout Managers:

  • FlowLayout: Places components in a row, aligned at their center.
  • BorderLayout: Divides the container into five areas: North, South, East, West, and Center.
  • GridLayout: Arranges components in a grid of cells, with each cell having the same size.
  • BoxLayout: Organizes components either vertically or horizontally.
  • GridBagLayout: A flexible layout manager that aligns components vertically, horizontally, or along their baseline without requiring the components to be of the same size.

Example: Using FlowLayout

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

public class FlowLayoutExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("FlowLayout Example");
        JPanel panel = new JPanel();
        
        panel.setLayout(new FlowLayout());
        
        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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

				
			

Example: Using BorderLayout

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

				
			

Example: Using GridLayout

				
					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, 2));
        
        frame.add(new JButton("Button 1"));
        frame.add(new JButton("Button 2"));
        frame.add(new JButton("Button 3"));
        frame.add(new JButton("Button 4"));
        
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

				
			
  • Containers like JFrame and JPanel are used to hold and organize other components.
  • Components like JButton, JLabel, and JTextField are the basic building blocks of a GUI.
  • Layout Managers like FlowLayout, BorderLayout, and GridLayout control the arrangement and size of components within a container.
Scroll to Top