Unit 4 Event Handling using AWT and Swing components
Awt:
1. AWT stands for Abstract Window Toolkit and they are used to create GUI applications
in Java.
2. The awt provides the following:
a. A full set of user interface (UI) widgets and other components, including
windows, menus, buttons, check boxes, text fields, scrollbars, and scrolling
list.
b. Support for UI containers, which can contain other embedded containers or UI
widgets.
c. An event system for managing system and user events among parts of the
awt.
d. Mechanisms for laying Management.
Component:
1. A Component is an object having a graphical representation that can be displayed
on the screen and that can interact with the user.
2. Component class is declared in java.awt package;
3. Examples of components are the Frame, Dialog , Buttons, checkboxes, and
scrollbars of a typical graphical user interface.
4. Note: Frame, Panel, Applet ,Dialog are Components as well as containers.
5. Methods of Component Class
a. void addComponentListener(ComponentListener l) - Adds the specified
component listener to receive component events from this component.
b. void add(Component c) - Add a component on another component.
c. void setVisible(boolean b) - Sets the visibility of the component. It is by
default false.
d. void setLayout(LayoutManager m) - Sets the layout manager for the
component.
6. Program to demonstrate Methods of Component class
import java.awt.*;
class MyFrame extends Frame {
MyFrame() {
setSize(600, 600);
setTitle("Draw Frame");
setBackground(Color.RED);
setLocation(100, 100);
}
public static void main(String[] args) {
MyFrame f = new MyFrame();
f.setVisible(true);
Dimension d = f.getSize();
System.out.println("Width=" + d.width + " , Height= " +
d.height);
}
}