Vous êtes sur la page 1sur 17

Alpha Breathing

AWT

Java AWT(Abstract Window Toolkit) isan API


to develop GUI or window-based application
in java.
Java AWT components are platform-dependent
(i.e.) components are displayed according to the
view of operating system.
AWT is heavyweight (i.e.) its components uses
the resources of system.
The java.awt package provides classes for AWT
api
such
as
TextField,
Label,
TextArea,
RadioButton, CheckBox, Choice, List etc.

AWT Hierarchy

Container
The Container is a component in AWT that can contain another
components like buttons, textfields, labels etc. The classes that
extends Container class are known as container such as Frame,
Dialog and Panel.
Window
The window is the container that have no borders and menu bars.
You must use frame, dialog or another window for creating a
window.
Panel
The Panel is the container that doesn't contain title bar and menu
bars. It can have other components like button, textfield etc.
Frame
The Frame is the container that contain title bar and can have
menu bars. It can have other components like button, textfield
etc.

Methods of Component class

public void add(Component c)

public void setSize(int width,int height)

public void setLayout(LayoutManager m)

public void setVisible(boolean status)

Ways to create a Frame in AWT


To create simple awt example, you need a
frame.
Two Ways to create frame are: By extending Frame class (inheritance)
By creating the object of Frame class
(association)

By extending Frame class


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

11.
12.
13.
14.
15.

importjava.awt.*;
classFirstextendsFrame{
First(){
Buttonb=newButton("clickme");
b.setBounds(30,100,80,30);//settingbuttonposition

add(b);//addingbuttonintoframe
setSize(300,300);//framesize300widthand300height
setLayout(null);//nolayoutmanager
setVisible(true);//nowframewillbevisible,bydefaultits
notvisible
}
publicstaticvoidmain(Stringargs[]){
Firstf=newFirst();
}
}

Output

By creating the object of Frame


class
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.

importjava.awt.*;
classFirst2{
First2(){
Framef=newFrame();

Buttonb=newButton("clickme");
b.setBounds(30,50,80,30);

f.add(b);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
publicstaticvoidmain(Stringargs[]){
First2f=newFirst2();
}}

Formative Assessment
1. Give the abbreviation of AWT?
a.Applet Windowing Toolkit
b.Abstract Windowing Toolkit
c.Absolute Windowing Toolkit
d.None of the above
2. These two ways are used to create a Frame
By creating the object of Frame class (association)
By extending Frame class (inheritance)
a.True
b.False

Formative Assessment
Where are the following four methods
commonly used?
1)
2)
3)
4)

public
public
public
public

void
void
void
void

add(Component c)
setSize(int width,int height)
setLayout(LayoutManager m)
setVisible(boolean)

a.Graphics class
b.Component class
c.Both A & B
d.None of the above

Event Handling in Java


Changing the state of an object is
known as an event.
For example, click on button, dragging
mouse etc.
The java.awt.event package provides
many event classes and Listener interfaces
for event handling.

Two types of Event:

Foreground Event

Background Event

EXAMPLE:
ActionEvent - ActionListener
MouseEvent - MouseListener
ContainerEvent - ContainerListener
ComponentEvent -ComponentListener

(EventClass --- ListenerInterface)

Example for ActionListener

Declare an event handler class and specify that the class either
implements an ActionListener interface or extends a class that
implements an ActionListener interface.
For example:
public class MyClass implements ActionListener {
Register an instance of the event handler class as a listener on one
or more components.
For example:
someComponent.addActionListener(instanceOfMyClass);
Include code that implements the methods in listener interface.
For example:
public void actionPerformed(ActionEvent e)
{ ...//code that reacts to the action... }

Summary
AWT
Inheritance
Association
Event Handling
Types of Event

Discussion
Mind Map

Vous aimerez peut-être aussi