0% found this document useful (0 votes)
27 views

AWT Class44

The Abstract Window Toolkit (AWT) is an API that provides components to build graphical user interfaces in Java. AWT components are platform-dependent, displaying differently on different operating systems. The AWT hierarchy includes containers like Frame and Panel that can hold other components like buttons and text fields. Event handling in AWT involves registering components with listener interfaces and implementing listener methods.

Uploaded by

Kousik manna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

AWT Class44

The Abstract Window Toolkit (AWT) is an API that provides components to build graphical user interfaces in Java. AWT components are platform-dependent, displaying differently on different operating systems. The AWT hierarchy includes containers like Frame and Panel that can hold other components like buttons and text fields. Event handling in AWT involves registering components with listener interfaces and implementing listener methods.

Uploaded by

Kousik manna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

AWT

ANNWESHA BANERJEE
Introduction

Abstract Window Toolkit


API to develop GUI or window-based
applications in java.
Java AWT components are platform-dependent i.e.
components are displayed ac
The java.awt package provides classes for AWT api
such as TextField, Label, TextArea, RadioButton,
CheckBox, Choice, List etc.cording to the view of
operating system.
Java 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.
Useful Methods of Component class

Method Description

public void add(Component c) inserts a component on this


component.

public void setSize(int width,int sets the size (width and height) of
height) the component.

public void defines the layout manager for the


setLayout(LayoutManager m) component.

public void setVisible(boolean changes the visibility of the


status) component, by default false.

Method Description
Creating AWT Application

There are two ways to create a frame in AWT.


 By extending Frame class (inheritance)
 By creating the object of Frame class (association)
Example

import java.awt.*;
class Test_AWT extends Frame{
Test_AWT(){
Label l=new Label("Test AWT");
l.setBounds(20,100,200,200);
Button b1=new Button("CLICK");
b1.setBounds(30,100,80,30);
add(l);
add(b1);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(String[] aa){
Test_AWT t=new Test_AWT();
}
}
Event Handling

Changing the state of an object is known as an event.


The java.awt.event package provides many event
classes and Listener interfaces for event handling.
Java Event classes and Listener interfaces

Event Classes Listener Interfaces


ActionEvent ActionListener
MouseEvent MouseListener and MouseMotionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
Steps to perform Event Handling

Following steps are required to perform event


handling:
 Register the component with the Listener
Registration Methods

Component Method

Button public void addActionListener(ActionListener a){}

MenuItem public void addActionListener(ActionListener a){}

TextField public void addActionListener(ActionListener a){}


public void addTextListener(TextListener a){}

TextArea public void addTextListener(TextListener a){}

Checkbox public void addItemListener(ItemListener a){}

Choice public void addItemListener(ItemListener a){}

List public void addActionListener(ActionListener a){}


public void addItemListener(ItemListener a){}
Event Handling Code
import java.awt.*;
import java.awt.event.*;
class Test_AWT extends Frame implements ActionListener{
Test_AWT(){

Button b1=new Button("CLICK");


b1.setBounds(30,100,80,30);
add(b1);
b1.addActionListener(this);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
this.setBackground(Color.YELLOW);
}
public static void main(String[] aa){
Test_AWT t=new Test_AWT();
}
}
Example of Window Closing
import java.awt.*;
import java.awt.event.*;
class Test_AWT extends Frame implements ActionListener{
Test_AWT(){

Button b1=new Button("CLICK");


b1.setBounds(30,100,80,30);
add(b1);
b1.addActionListener(this);
setSize(300,300);
setLayout(null);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
this.setBackground(Color.YELLOW);
}
public static void main(String[] aa){
Test_AWT t=new Test_AWT();
}
}
Thank You

You might also like