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!). SetFlowLayoutfor 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.