Introduction to AWT and Applets · Introduction to Java Programming

Creating a Frame

Watch lectures

Creating a Frame

1. What Is a Frame? (beginner)

Frame = top window with title bar + border (close/minimize!). Main AWT window (java.awt.Frame). Holds buttons/labels via layout!

2. Two Ways (exam both!)

// WAY 1: use Frame directly (quick!)
import java.awt.*;
Frame f = new Frame("Bill App");
f.add(new Label("total: 150"));
f.setSize(300, 200);
f.setLayout(new FlowLayout());
f.setVisible(true);

// WAY 2: extend Frame (custom window class! — preferred for big apps!)
class MyWin extends Frame {
    MyWin() {
        super("My Win"); // title!
        add(new Button("OK"));
        setSize(300, 200);
        setVisible(true);
    }
}
new MyWin(); // build!

3. Must-Call Trio (forget = broken window!)

setSize(300,200) (else dot!) + setLayout(...) (else Border pile!) + setVisible(true) (else hidden!).

4. Traps

  • Default layout BorderLayout (all pile CENTER without layout!). Set FlowLayout for beginners!
  • Frame has menu bar support (MenuBar); Panel doesn't!
  • Close button does NOTHING by default (next topic: closing!).

5. Exam Points

  • Two ways + trio program — 5 marks, very frequent.
  • Layout/visible traps — 3 marks, frequent.
Student FinanceWeb Platform

Pocketly — Personal Finance for Students

Keep your hostel and college expenses under control. Track accounts, log spending in seconds, and stay within your monthly budget.

← All Introduction to Java Programming modules