0% found this document useful (0 votes)
6 views20 pages

Java Abstract Window Toolkit (AWT)

Java Abstract Window Toolkit (AWT) is an API for creating and managing graphical user interface (GUI) applications with a hierarchy of classes such as Component, Container, and Frame. It includes various components like Buttons, Labels, TextFields, and more, which can be added to frames for user interaction. The document provides examples of how to create and use these components in Java applications.

Uploaded by

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

Java Abstract Window Toolkit (AWT)

Java Abstract Window Toolkit (AWT) is an API for creating and managing graphical user interface (GUI) applications with a hierarchy of classes such as Component, Container, and Frame. It includes various components like Buttons, Labels, TextFields, and more, which can be added to frames for user interaction. The document provides examples of how to create and use these components in Java applications.

Uploaded by

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

Java Abstract Window Toolkit(AWT)

Java AWT is an API that contains large number of classes and methods to
create and manage graphical user interface ( GUI ) applications. The AWT was
designed to provide a common set of tools for GUI design that could work on a
variety of platforms.

Java AWT Hierarchy

The hierarchy of Java AWT classes are given below, all the classes are
available in java.awt package.

Component class

Component class is at the top of AWT hierarchy. It is an abstract class


that encapsulates all the attributes of visual component. A component object is
responsible for remembering the current foreground and background colors and
the currently selected text font.

Container

Container is a component in AWT that contains another component like


button, text field, tables etc. Container is a subclass of component
class. Container class keeps track of components that are added to another
component.

Panel

Panel class is a concrete subclass of Container. Panel does not contain


title bar, menu bar or border. It is container that is used for holding components.

Window class

Window class creates a top level window. Window does not have borders
and menubar.

Frame

Frame is a subclass of Window and have resizing canvas. It is a container that


contain several different components like button, title bar, textfield, label etc. In
Java, most of the AWT applications are created using Frame window. Frame
class has two different constructors,
There are two ways to create a Frame. They are,

1. By Instantiating Frame class


2. By extending Frame class

Creating Frame Window by Instantiating Frame class


import java.awt.*;
public class Testawt
{
Testawt()
{
Frame fm=new Frame(); //Creating a frame
Label lb = new Label("welcome to java graphics"); //Creating a label
fm.add(lb); //adding label to the frame
fm.setSize(300, 300); //setting frame size.
fm.setVisible(true); //set frame visibilty true
}
public static void main(String args[])
{
Testawt ta = new Testawt();
}
}

Creating Frame window by extending Frame class


import java.awt.*;
import java.awt.event.*;

public class Testawt extends Frame


{
public Testawt()
{
Button btn=new Button("Hello World");
add(btn); //adding a new Button.
setSize(400, 500); //setting size.
setTitle("StudyTonight"); //setting title.
setLayout(new FlowLayout()); //set default layout for frame.
setVisible(true); //set frame visibilty true.
}

public static void main (String[] args)


{
Testawt ta = new Testawt(); //creating a frame.
}
}

Points to Remember:

1. While creating a frame (either by instantiating or extending Frame class),


Following two attributes are must for visibility of the frame:
o setSize(int width, int height);
o setVisible(true);
2. When you create other components like Buttons, TextFields, etc. Then
you need to add it to the frame by using the method - add(Component's
Object);
3. You can add the following method also for resizing the frame
- setResizable(true);

AWT Button
In Java, AWT contains a Button Class. It is used for creating a labelled
button which can perform an action.

AWT Button Classs Declaration:


public class Button extends Component implements Accessible

import java.awt.*;
public class ButtonDemo1
{
public static void main(String[] args)
{
Frame f1=new Frame("studytonight ==> Button Demo");
Button b1=new Button("Press Here");
b1.setBounds(80,200,80,50);
f1.add(b1);
f1.setSize(500,500);
f1.setLayout(null);
f1.setVisible(true);
}
}
AWT Label
In Java, AWT contains a Label Class. It is used for placing text in a
container. Only Single line text is allowed and the text can not be changed
directly.

Label Declaration:
public class Label extends Component implements Accessible

import java.awt.*;
class LabelDemo1
{
public static void main(String args[])
{
Frame l_Frame= new Frame("studytonight ==> Label Demo");
Label lab1,lab2;
lab1=new Label("Welcome to studytonight.com");
lab1.setBounds(50,50,200,30);
lab2=new Label("This Tutorial is of Java");
lab2.setBounds(50,100,200,30);
l_Frame.add(lab1);
l_Frame.add(lab2);
l_Frame.setSize(500,500);
l_Frame.setLayout(null);
l_Frame.setVisible(true);
}
}

AWT TextField

In Java, AWT contains aTextField Class. It is used for displaying single


line text.
TextField Declaration:
public class TextField extends TextComponent

import java.awt.*;
class TextFieldDemo1{
public static void main(String args[]){
Frame TextF_f= new Frame("studytonight ==>TextField");
TextField text1,text2;
text1=new TextField("Welcome to studytonight");
text1.setBounds(60,100, 230,40);
text2=new TextField("This tutorial is of Java");
text2.setBounds(60,150, 230,40);
TextF_f.add(text1);
TextF_f.add(text2);
TextF_f.setSize(500,500);
TextF_f.setLayout(null);
TextF_f.setVisible(true);
}
}
AWT TextArea
In Java, AWT contains aTextArea Class. It is used for displaying
multiple-line text.
TextArea Declaration:
public class TextArea extends TextComponent

import java.awt.*;
public class TextAreaDemo1
{
TextAreaDemo1()
{
Frame textArea_f= new Frame();
TextArea area=new TextArea("Welcome to studytonight.com");
area.setBounds(30,40, 200,200);
textArea_f.add(area);
textArea_f.setSize(300,300);
textArea_f.setLayout(null);
textArea_f.setVisible(true);
}
public static void main(String args[])
{
new TextAreaDemo1();
}
}

AWT Checkbox
In Java, AWT contains a Checkbox Class. It is used when we want to
select only one option i.e true or false. When the checkbox is checked then its
state is "on" (true) else it is "off"(false).
Checkbox Syntax
public class Checkbox extends Component implements ItemSelectable,
Accessible
import java.awt.*;
public class CheckboxDemo1
{
CheckboxDemo1(){
Frame checkB_f= new Frame("studytonight ==>Checkbox Example");
Checkbox ckbox1 = new Checkbox("Yes", true);
ckbox1.setBounds(100,100, 60,60);
Checkbox ckbox2 = new Checkbox("No");
ckbox2.setBounds(100,150, 60,60);
checkB_f.add(ckbox1);
checkB_f.add(ckbox2);
checkB_f.setSize(400,400);
checkB_f.setLayout(null);
checkB_f.setVisible(true);
}
public static void main(String args[])
{
new CheckboxDemo1();
}
}

AWT CheckboxGroup
In Java, AWT contains aCheckboxGroup Class. It is used to group a set
of Checkbox. When Checkboxes are grouped then only one box can be checked
at a time.
CheckboxGroup Declaration:
public class CheckboxGroup extends Object implements Serializable

import java.awt.*;
public class CheckboxGroupDemo
{
CheckboxGroupDemo(){
Frame ck_groupf= new Frame("studytonight ==>CheckboxGroup");
CheckboxGroupobj = new CheckboxGroup();
Checkbox ckBox1 = new Checkbox("Yes", obj, true);
ckBox1.setBounds(100,100, 50,50);
Checkbox ckBox2 = new Checkbox("No", obj, false);
ckBox2.setBounds(100,150, 50,50);
ck_groupf.add(ckBox1);
ck_groupf.add(ckBox2);
ck_groupf.setSize(400,400);
ck_groupf.setLayout(null);
ck_groupf.setVisible(true);
}
public static void main(String args[])
{
new CheckboxGroupDemo();
}
}

AWT Choice
In Java, AWT contains a Choice Class. It is used for creating a drop-
down menu of choices. When a user selects a particular item from the drop-
down then it is shown on the top of the menu.
Choice Declaration:
public class Choice extends Component implements ItemSelectable, Accessible

import java.awt.*;
public class ChoiceDemo
{
ChoiceDemo()
{
Frame choice_f= new Frame();
Choice obj=new Choice();
obj.setBounds(80,80, 100,100);
obj.add("Red");
obj.add("Blue");
obj.add("Black");
obj.add("Pink");
obj.add("White");
obj.add("Green");
choice_f.add(obj);
choice_f.setSize(400,400);
choice_f.setLayout(null);
choice_f.setVisible(true);
}
public static void main(String args[])
{
new ChoiceDemo();
}
}
AWT List
In Java, AWT contains a List Class. It is used to represent a list of items
together. One or more than one item can be selected from the list.
List Declaration:
public class List extends Component implements ItemSelectable, Accessible

import java.awt.*;
public class ListDemo
{
ListDemo()
{
Frame list_f= new Frame();
List obj=new List(6);
obj.setBounds(80,80, 100,100);
obj.add("Red");
obj.add("Blue");
obj.add("Black");
obj.add("Pink");
obj.add("White");
obj.add("Green");
list_f.add(obj);
list_f.setSize(400,400);
list_f.setLayout(null);
list_f.setVisible(true);
}
public static void main(String args[])
{
new ListDemo();
}
}

AWT Canvas
In Java, AWT contains a Canvas Class. A blank rectangular area is
provided. It is used when a user wants to draw on the screen.
Declaration:
public class Canvas extends Component implements Accessible

import java.awt.*;
public class CanvasDemo1
{
public CanvasDemo1()
{
Frame canvas_f= new Frame("studytonight ==> Canvas");
canvas_f.add(new CanvasDemo());
canvas_f.setLayout(null);
canvas_f.setSize(500, 500);
canvas_f.setVisible(true);
}
public static void main(String args[])
{
new CanvasDemo1();
}
}
class CanvasDemo extends Canvas
{
public CanvasDemo() {
setBackground (Color.WHITE);
setSize(300, 200);
}
public void paint(Graphics g)
{
g.setColor(Color.green);
g.fillOval(80, 80, 150, 75);
}
}

AWT MenuItem and Menu


In Java, AWT contains a MenuItem and Menu Class.MenuItem is used
for adding Lable in Menu. The menu is used to create a drop-down of menu
components
MenuItem declaration
public class MenuItem extends MenuComponent implements Accessible
Menu declaration
public class Menu extends MenuItem implements MenuContainer, Accessible

import java.awt.*;
class MenuDemo1
{
MenuDemo1()
{
Frame menu_f= new Frame("studytonight ==> Menu and MenuItem Demo");
MenuBarmenu_bar=new MenuBar();
Menu menu11=new Menu("Menu");
Menu sub_menu1=new Menu("Sub Menu =>");
MenuItem a1=new MenuItem("Red");
MenuItem a2=new MenuItem("Light Red");
MenuItem a3=new MenuItem("Drak Red");
MenuItem b1=new MenuItem("Green");
MenuItem b2=new MenuItem("Light Green");
MenuItem b3=new MenuItem("Dark Green");
menu11.add(a1);
sub_menu1.add(a2);
sub_menu1.add(a3);
menu11.add(b1);
sub_menu1.add(b2);
sub_menu1.add(b3);
menu11.add(sub_menu1);
menu_bar.add(menu11);
menu_f.setMenuBar(menu_bar);
menu_f.setSize(400,400);
menu_f.setLayout(null);
menu_f.setVisible(true);
}
public static void main(String args[])
{
new MenuDemo1();
}
}

AWT PopupMenu
In Java, AWT contains a Popup Menu. It is a popup which is dynamic in
nature.
PopupMenu declaration
public class PopupMenu extends Menu implements MenuContainer, Accessible

import java.awt.*;
import java.awt.event.*;
class PopupMenuDemo1
{
PopupMenuDemo1()
{
final Frame pop_menuf= new Frame("studytonight ==>PopupMenu Demo");
final PopupMenupop_menu = new PopupMenu("*Edit*");
MenuItempop_cut = new MenuItem("Cut");
pop_cut.setActionCommand("Cut");
MenuItempop_copy = new MenuItem("Copy");
pop_copy.setActionCommand("Copy");
MenuItempop_paste = new MenuItem("Paste");
pop_paste.setActionCommand("Paste");
pop_menu.add(pop_cut);
pop_menu.add(pop_copy);
pop_menu.add(pop_paste);
pop_menuf.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent a)
{
pop_menu.show(pop_menuf , a.getX(), a.getY());
}
});
pop_menuf.add(pop_menu);
pop_menuf.setSize(400,400);
pop_menuf.setLayout(null);
pop_menuf.setVisible(true);
}
public static void main(String args[])
{
new PopupMenuDemo1();
}
}

AWT Panel
In Java, AWT contains a Panel. The panel provides a free space where
components can be placed.
Panel declaration
public class Panel extends Container implements Accessible

import java.awt.*;
public class PanelDemo1{
PanelDemo1()
{
Frame panel_f= new Frame("studytonight ==> Panel Demo");
Panel panel11=new Panel();
panel11.setBounds(40,80,200,200);
panel11.setBackground(Color.red);
Button box1=new Button("On");
box1.setBounds(50,100,80,30);
box1.setBackground(Color.gray);
Button box2=new Button("Off");
box2.setBounds(100,100,80,30);
box2.setBackground(Color.gray);
panel11.add(box1);
panel11.add(box2);
panel_f.add(panel11);
panel_f.setSize(400,400);
panel_f.setLayout(null);
panel_f.setVisible(true);
}
public static void main(String args[])
{
new PanelDemo1();
}
}

AWT Dialog
In Java, AWT contains a Dialog. It is a type of window which is having a
border and a title. But it does not have any maximize and minimize button.
Declaration
public class Dialog extends Window

import java.awt.*;
import java.awt.event.*;
public class DialogDemo1
{
private static Dialog dialog_d;
DialogDemo1()
{
Frame dialog_f= new Frame();
dialog_d = new Dialog(dialog_f , "studytonight ==> Dialog Demo", true);
dialog_d.setLayout( new FlowLayout() );
Button dialog_b = new Button ("OK");
dialog_b.addActionListener ( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
DialogDemo1.dialog_d.setVisible(false);
}
});
dialog_d.add( new Label ("Welcome to studytonight. Click on button to
continue."));
dialog_d.add(dialog_b);
dialog_d.setSize(300,300);
dialog_d.setVisible(true);
}
public static void main(String args[])
{
new DialogDemo1();
}
}

AWT Toolkit
In Java, AWT contains a Toolkit. It is a superclass of Abstract Window
Toolkit and can be implemented anywhere.
Declaration
public abstract class Toolkit extends Object

import java.awt.*;
import java.awt.event.*;
public class ToolkitDemo1
{
public static void main(String[] args)
{
Frame toolkit_f=new Frame("studytonight ==> Toolkit Demo");
Button toolkit_b=new Button("beep");
toolkit_b.setBounds(50,100,60,30);
toolkit_f.add(toolkit_b);
toolkit_f.setSize(300,300);
toolkit_f.setLayout(null);
toolkit_f.setVisible(true);
toolkit_b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent a)
{
Toolkit.getDefaultToolkit().beep();
}
});
}
}

ActionListener Interface
In java, ActionListener Interface is present under java.awt.event
package. This interface is used when you want to notify click action on button
or menu item. It has actionPeformed() method.
Syntax
public abstract void actionPerformed(ActionEvent e)
Following are the three steps to add ActionListener Interface
Step 1: Implement the ActionListener Interface in the class.
Syntax:
public class ActionListenerDemo Implements ActionListener
Step 2: Now Register all the components with the Listener.
Syntax:
component.addActionListener(instanceOfListenerclass);
Step 3: Aylast override the actionPerformed() method.
Syntax:

public void actionPerformed(ActionEvent e)


{
//statements
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class ActionListenerDemo1 implements ActionListener {

JButton aL_button;
JFrame aL_frame;
JTextArea aL_textArea;

public ActionListenerDemo1() {
aL_button = new JButton("Click Me");
aL_frame = new JFrame("studytonight ==>ActionListener Demo");
aL_textArea = new JTextArea(50, 50);
aL_button.addActionListener(this);
aL_textArea.setLineWrap(true);
aL_frame.setLayout(new BorderLayout());
aL_frame.add(aL_textArea, BorderLayout.NORTH);
aL_frame.add(aL_button, BorderLayout.SOUTH);
aL_frame.pack();

aL_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aL_frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
aL_textArea.setText(aL_textArea.getText().concat("Welocme to
studytonight.com\n"));
}

public static void main(String args[]) {


ActionListenerDemo1 obj= new ActionListenerDemo1();
}
}

MouseListener Interface
In Java, MouseListener Interface is under java.awt.event package.This
interface is used when the state of the mouse is changed. It has 5 methods
which are as following:
1. public abstract void mouseClicked(MouseEvent e);
2. public abstract void mouseEntered(MouseEvent e);
3. public abstract void mouseExited(MouseEvent e);
4. public abstract void mousePressed(MouseEvent e);
5. public abstract void mouseReleased(MouseEvent e);

import java.awt.*;
import java.awt.event.*;
public class MouseListenerDemo1 extends Frame implements MouseListener{
Label mL_l;
MouseListenerDemo1(){
addMouseListener(this);

mL_l=new Label();
mL_l.setBounds(10,20,500,100);
add(mL_l);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
mL_l.setText("studytonight ==> Mouse Clicked");
}
public void mouseEntered(MouseEvent e) {
mL_l.setText("studytonight ==> Mouse Entered");
}
public void mouseExited(MouseEvent e) {
mL_l.setText("studytonight ==> Mouse Exited");
}
public void mousePressed(MouseEvent e) {
mL_l.setText("studytonight ==> Mouse Pressed");
}
public void mouseReleased(MouseEvent e) {
mL_l.setText("studytonight ==> Mouse Released");
}
public static void main(String[] args) {
new MouseListenerDemo1();
}
}

MouseMotionListener Interface
In Java, MouseMotionListener Interface is
under java.awt.event package.This interface is used whenever the mouse is
moved or dragged. It has 2 methods which are as following:
1. public abstract void mouseDragged(MouseEvent e);
2. public abstract void mouseMoved(MouseEvent e);

import java.awt.*;
import java.awt.event.*;
public class MouseMotionListenerDemo1 extends Frame implements
MouseMotionListener{
MouseMotionListenerDemo1(){
addMouseMotionListener(this);

setSize(500,500);
setLayout(null);
setVisible(true);
}
public void mouseDragged(MouseEvent a) {
Graphics mM_g=getGraphics();
mM_g.setColor(Color.ORANGE);
mM_g.fillOval(a.getX(),a.getY(),10,10);
}
public void mouseMoved(MouseEvent e) {}

public static void main(String[] args) {


new MouseMotionListenerDemo1();
}
}

ItemListener Interface
In Java, ItemListener Interface is under java.awt.event package. This
interface is used whenever the checkbox is clicked. It has itemStateChanged()
method.
Syntax:
public abstract void itemStateChanged(ItemEvent e)

import java.awt.*;
import java.awt.event.*;
public class ItemListenerDemo1 implements ItemListener{
Checkbox iT_checkBox1,iT_checkBox2;
Label iT_label;
ItemListenerDemo1(){
Frame iT_f= new Frame("studytonight ==>CheckBox Demo");
iT_label = new Label();
iT_label.setAlignment(Label.CENTER);
iT_label.setSize(400,100);
iT_checkBox1 = new Checkbox("Core Java");
iT_checkBox1.setBounds(100,100, 100,40);
iT_checkBox2 = new Checkbox("jsp");
iT_checkBox2.setBounds(100,150, 100,40);
iT_f.add(iT_checkBox1);
iT_f.add(iT_checkBox2);
iT_f.add(iT_label);
iT_checkBox1.addItemListener(this);
iT_checkBox2.addItemListener(this);
iT_f.setSize(400,400);
iT_f.setLayout(null);
iT_f.setVisible(true);
}
public void itemStateChanged(ItemEventiT) {
if(iT.getSource()==iT_checkBox1)
iT_label.setText("Core Java Checkbox: "+
(iT.getStateChange()==1?"checked":"unchecked"));
if(iT.getSource()==iT_checkBox2)
iT_label.setText("jsp Checkbox: "+
(iT.getStateChange()==1?"checked":"unchecked"));
}
public static void main(String args[])
{
new ItemListenerDemo1();
}
}

KeyListener Interface
In Java, KeyListener Interface is under java.awt.event package. This
interface is used when the state of the key is changed. It has 3 methods which
are as following:
1. public abstract void keyPressed(KeyEvent e);
2. public abstract void keyReleased(KeyEvent e);
3. public abstract void keyTyped(KeyEvent e);

import java.awt.*;
import java.awt.event.*;
public class KeyListenerDemo1 extends Frame implements KeyListener{
Label kL_l;
TextArea kL_area;
KeyListenerDemo1(){

kL_l=new Label();
kL_l.setBounds(20,50,500,20);
kL_area=new TextArea();
kL_area.setBounds(20,80,300, 300);
kL_area.addKeyListener(this);

add(kL_l);
add(kL_area);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void keyPressed(KeyEvent e) {
kL_l.setText("studytonight ==> Key Pressed");
}
public void keyReleased(KeyEvent e) {
kL_l.setText("studytonight ==> Key Released");
}
public void keyTyped(KeyEvent e) {
kL_l.setText("studytonight ==> Key Typed");
}

public static void main(String[] args) {


new KeyListenerDemo1();
}
}

WindowListener Interface
In Java, WindowListener Interface is under java.awt.event package. This
interface is used when the state of the window is changed. It has 7 methods
which are as following:
1. public abstract void windowActivated(WindowEvent e);
2. public abstract void windowClosed(WindowEvent e);
3. public abstract void windowClosing(WindowEvent e);
4. public abstract void windowDeactivated(WindowEvent e);
5. public abstract void windowDeiconified(WindowEvent e);
6. public abstract void windowIconified(WindowEvent e);
7. public abstract void windowOpened(WindowEvent e);

import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class WindowDemo1 extends Frame implements WindowListener
{
WindowDemo1()
{
addWindowListener(this);
setSize(500,500);
setLayout(null);
setVisible(true);
}

public static void main(String[] args)


{
new WindowDemo1();
}
public void windowActivated(WindowEvent arg0)
{
System.out.println("studytonight ==> activated");
}
public void windowClosed(WindowEvent arg0)
{
System.out.println("studytonight ==> closed");
}
public void windowClosing(WindowEvent arg0)
{
System.out.println("studytonight ==> closing");
dispose();
}
public void windowDeactivated(WindowEvent arg0)
{
System.out.println("studytonight ==> deactivated");
}
public void windowDeiconified(WindowEvent arg0)
{
System.out.println("studytonight ==>deiconified");
}
public void windowIconified(WindowEvent arg0)
{
System.out.println("studytonight ==>iconified");
}
public void windowOpened(WindowEvent arg0)
{
System.out.println("studytonight ==> opened");
}
}

You might also like