π GUI Programming in Java using AWT (Complete Guide with Layout Managers & Programs)
π Introduction
In earlier programs, we interacted with users using the console (CLI) β typing inputs and printing outputs. But modern applications use Graphical User Interfaces (GUI) π¨
π GUI allows users to interact with applications using:
- Buttons π
- Text fields β¨οΈ
- Windows πͺ
- Menus π
π§ Why GUI?
- Easy to use π
- Interactive π―
- Professional look π»
- Improves user experience π
βοΈ CLI vs GUI
| Feature | CLI | GUI |
|---|---|---|
| Input | Keyboard | Mouse + Keyboard |
| Ease of use | Hard | Easy |
| Interaction | Low | High |
π§ What is AWT?
π AWT (Abstract Window Toolkit) is Javaβs first GUI framework.
- Part of
java.awtpackage - Provides components like Button, Label, TextField
- Uses native OS components β called Heavyweight
π§± Basic Structure of AWT Program
β First GUI Program (Complete Runnable Code)
import java.awt.Frame;
public class FirstAWTProgram {
public static void main(String[] args) {
Frame frame = new Frame("My First AWT App");
frame.setSize(400, 300); // width, height
frame.setVisible(true);
}
}
π Explanation
Frameβ Window container πͺsetSize()β Sets window sizesetVisible(true)β Makes it visible
π§© AWT Components
πΉ 1. Button
import java.awt.*;
public class ButtonDemo {
public static void main(String[] args) {
Frame frame = new Frame("Button Example");
Button btn = new Button("Click Me");
frame.add(btn);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
π Explanation
Buttonβ Clickable component πframe.add()β Adds component to frame
πΉ 2. Label
import java.awt.*;
public class LabelDemo {
public static void main(String[] args) {
Frame frame = new Frame("Label Example");
Label label = new Label("Welcome to AWT");
frame.add(label);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
πΉ 3. TextField
import java.awt.*;
public class TextFieldDemo {
public static void main(String[] args) {
Frame frame = new Frame("TextField Example");
TextField tf = new TextField();
frame.add(tf);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
π§± Layout Managers (VERY IMPORTANT)
π Layout managers control how components are arranged.
πΉ 1. FlowLayout (Default)
π Features
- Left to right arrangement
- Automatically wraps components
β Program
import java.awt.*;
public class FlowLayoutDemo {
public static void main(String[] args) {
Frame frame = new Frame("FlowLayout Example");
frame.setLayout(new FlowLayout());
frame.add(new Button("One"));
frame.add(new Button("Two"));
frame.add(new Button("Three"));
frame.setSize(400, 200);
frame.setVisible(true);
}
}
π Output Behavior
π Buttons appear in a row β wrap if needed
πΉ 2. GridLayout
π Features
- Rows & columns
- Equal-sized components
β Program
import java.awt.*;
public class GridLayoutDemo {
public static void main(String[] args) {
Frame frame = new Frame("GridLayout Example");
frame.setLayout(new GridLayout(2, 2)); // 2 rows, 2 columns
frame.add(new Button("1"));
frame.add(new Button("2"));
frame.add(new Button("3"));
frame.add(new Button("4"));
frame.setSize(300, 300);
frame.setVisible(true);
}
}
π Explanation
- Components are arranged in grid format
- All cells are equal
πΉ 3. BorderLayout
π Features
- Divides screen into 5 regions:
- North
- South
- East
- West
- Center
β Program
import java.awt.*;
public class BorderLayoutDemo {
public static void main(String[] args) {
Frame frame = new Frame("BorderLayout Example");
frame.setLayout(new BorderLayout());
frame.add(new Button("North"), BorderLayout.NORTH);
frame.add(new Button("South"), BorderLayout.SOUTH);
frame.add(new Button("East"), BorderLayout.EAST);
frame.add(new Button("West"), BorderLayout.WEST);
frame.add(new Button("Center"), BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
π Explanation
- Each region holds one component
- Center takes remaining space
π οΈ Mini GUI Project (Combining Concepts)
β Complete Program
import java.awt.*;
public class MiniAWTApp {
public static void main(String[] args) {
Frame frame = new Frame("Mini AWT App");
frame.setLayout(new FlowLayout());
Label label = new Label("Enter Name:");
TextField tf = new TextField(20);
Button btn = new Button("Submit");
frame.add(label);
frame.add(tf);
frame.add(btn);
frame.setSize(400, 200);
frame.setVisible(true);
}
}
π What This Shows
- Multiple components working together
- Real-world UI structure
β οΈ Common Mistakes Students Make
β Forgetting setVisible(true)
π Window will not appear
β Not setting layout
π Components may overlap
β Adding too many components without layout
π Messy UI
β Ignoring window closing
π Program keeps running in background
π€― Common Confusions
π€ Why AWT is called Heavyweight?
π Uses native OS components
π€ Why layout managers needed?
π Without layout β components overlap
πΌ Interview Questions
β What is AWT?
π Java GUI toolkit using native components
β What is FlowLayout?
π Default layout arranging components left to right
β Difference between GridLayout and BorderLayout?
π Grid β rows/columns
π Border β regions
β What is Frame?
π Top-level window container
π― Best Practices
βοΈ Always use layout managers
βοΈ Keep UI simple
βοΈ Use proper component sizes
βοΈ Avoid null layout
π Conclusion
AWT is the foundation of Java GUI programming π―
In this blog, you learned:
- βοΈ What GUI is
- βοΈ AWT basics
- βοΈ Components (Button, Label, TextField)
- βοΈ Layout Managers (Flow, Grid, Border)
- βοΈ Building real GUI apps
π‘ Final Thought
π Master AWT basics first, then move to Swing (modern GUI) π
π₯ One-Line Summary
π βAWT provides basic tools to build GUI applications in Java using components and layouts.β
π» Happy Coding! π
Now youβre ready to build real GUI apps π₯