Week 2 Handout 1
Week 2 Handout 1
import java.awt.*;
import java.awt.event.*;
frame.add(button);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}
Types of Containers:
Top-Level Containers:
Frame: A window with a title bar and border.
Dialog: A pop-up window for user interaction.
Intermediate Containers:
Panel: A generic container used for organizing components.
import java.awt.*;
// Create components
Button button = new Button("Submit");
TextField textField = new TextField("Enter text here");
Label label = new Label("This is a label");
// Set positions
label.setBounds(50, 50, 150, 20);
textField.setBounds(50, 100, 150, 20);
button.setBounds(50, 150, 80, 30);
// Frame settings
frame.setSize(300, 300);
frame.setLayout(null);
frame.setVisible(true);
}
}
Layout Managers
Layout managers control the arrangement of components within a container.
import java.awt.*;
// Set layout
frame.setLayout(new FlowLayout());
// Add components
frame.add(new Button("Button 1"));
frame.add(new Button("Button 2"));
frame.add(new Button("Button 3"));
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Event Listener:
An object that listens for events.
Event Object:
Contains information about the event.
import java.awt.*;
import java.awt.event.*;
Lab Activity 3
Create an AWT application with:
A TextField for user input.
Two buttons labeled "Show" and "Clear."
When "Show" is clicked, display the text in the console.
When "Clear" is clicked, clear the TextField.