Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Java Abstract Window Toolkit

Java Abstract Window Toolkit (AWT) is an API for creating and managing graphical user interface (GUI) applications, providing a set of platform-dependent tools. It includes various components like buttons, labels, text fields, and more, organized in a class hierarchy, and supports event handling for user interactions. AWT allows developers to create GUI applications through inheritance or object creation, and includes examples demonstrating the use of its components and event handling mechanisms.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Abstract Window Toolkit

Java Abstract Window Toolkit (AWT) is an API for creating and managing graphical user interface (GUI) applications, providing a set of platform-dependent tools. It includes various components like buttons, labels, text fields, and more, organized in a class hierarchy, and supports event handling for user interactions. AWT allows developers to create GUI applications through inheritance or object creation, and includes examples demonstrating the use of its components and event handling mechanisms.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

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 Provides 2 Frameworks for building GUI-based applications. Those are
 AWT-(Abstract Window Toolkit)
 Swing
AWT (Abstract Window Toolkit) is an API to develop GUI or window-based
applications in java.
• The java.awt package provides classes for AWT API such as TextField, Label,
TextArea, Checkbox, Choice, List etc.
• AWT components are platform-dependent i.e., components are displayed
according to the view of operating system

Java AWT Hierarchy


The hierarchy of Java AWT classes are given below, all the classes are available
in java.awt package.
Component:
Component is an abstract class that contains various classes such as Button,
Label, Checkbox, TextField, Menu and etc.
• Container:
The Container is a component in AWT that can contain other components like
buttons, textfields, labels etc. The Container class extends Frame and Panel.
Window:
The window is the container that have no borders and menu bars. You must use
frame for creating a window.
• Frame:
The Frame is the container that contain title bar and can have menu bars. It can
have other components like button, textfield etc.
• Panel:
The Panel is the container that doesn't contain title bar and menu bars. It can
have other components like button, textfield etc.
Commonly used Methods of Component class
add(Component c) inserts a component on this component.
setSize(int width,int height) sets the size (width and height) of the component.
setLayout(LayoutManager m) defines the layout manager for the component.
setVisible(boolean status) changes the visibility of the component, by default
false.

To create simple awt example, you need a frame.


There are two ways to create a frame in AWT.
• By extending Frame class (inheritance)
Ex:
class Example extends Frame
{
……..
}
• By creating the object of Frame class (association)
Ex:
class Example
{
Frame obj=new Frame();
……..
}

A Simple AWT Example:


import java.awt.*;
public class AwtExample
{
public static void main(String[] args)
{
Frame f=new Frame();
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
f.setTitle("Simple Example");
}
}
AWT Components or Elements:
• Button:
The button class is used to create a labeled button that has platform independent
implementation. The application result in some action when the button is
pushed.
Syntax:
Button b=new Button(“Text");
(Or)
Button b1, b2;
b1=new Button(“Text”);
b1.setBounds(50,100,80,30);
setBounds(int x,int y,int width,int height)
This method is used to declare location, width & height of all components of
AWT.
Example: setBounds(50,100,80,30);
Label:
The Label class is a component for placing text in a container. It is used to
display
a single line of read only text. The text can be changed by an application but a
user
cannot edit it directly.
Syntax:
Label l1=new Label(“Text”);
(or)
Label l1,l2;
l1=new Label(“Text”);
TextField:
The TextField class is a text component that allows the editing of a single line
text.
Syntax:
TextField t1=new TextField(“Text”);
(or)
TextField t1,t2;
t1=new TextField(“Text”);
• TextArea :
The TextArea class is a multi line region that displays text. It allows the editing
of
multiple line text.
Syntax:
TextArea t1=new TextArea(“Text”);
(or)
TextArea t1,t2;
t1=new TextArea(“Text”);
• Checkbox :
The Checkbox class is used to create a checkbox. It is used to turn an option on
(true) or off (false). Clicking on a Checkbox changes its state from "on" to "off"
or from
"off" to "on".
Syntax:
Checkbox c1=new Checkbox(“Text”);
(or)
Checkbox c1,c2;
c1=new Checkbox(“Text”);
List :
The List class represents a list of text items. By the help of list, user can choose
either one item or multiple items.
Syntax:
List ls=new List(Size);
ls.add("Item 1");
ls.add("Item 2");
ls.add("Item 3");
Example: An example for Button Component in AWT.

import java.awt.*;
import java.awt.event.*;
class ButtonExample
{
public static void main(String[] args)
{
// creation of Frame
Frame f=new Frame();
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
f.setTitle("ButtonExample");
//creation of Button
Button b=new Button("SUBMIT");
b.setBounds(150,200,95,30);
f.add(b);
//Code for Close window
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
}
AWT does allow the user to close window directly…
We need code to close window
//Code for Close window
FrameName. addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
Note: We need to import one package “java.awt.event.*”.

we are creating two labels to display text to the frame


import java.awt.*;
class LabelDemo1
{
public static void main(String args[])
{
Frame l_Frame= new Frame("JIMS BCA CTIS &DS");
Label lab1,lab2;
lab1=new Label("Welcome to JIMS");
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);
}
}

TextField Example
import java.awt.*;
class TextFieldDemo1{
public static void main(String args[]){
Frame TextF_f= new Frame("TextField");
TextField text1, text2;
text1=new TextField("Welcome to JIMS");
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

Example:

In this example, we are creating a TextArea that is used to display multiple-line


text string and allows text editing as well.

import java.awt.*;
public class TextAreaDemo1
{
TextAreaDemo1()
{
Frame textArea_f= new Frame();
TextArea area=new TextArea("Welcome to JIMS");
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

Example:

In this example, we are creating checkbox that are used to get user input. If
checkbox is checked it returns true else returns false.

import java.awt.*;
public class CheckboxDemo1
{
CheckboxDemo1(){
Frame checkB_f= new Frame("Welcome to JIMS ");
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 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

Example:

In this example, we are creating drop-down menu that is used to get user choice
from multiple choices.

import java.awt.*;
import java.awt.event.*;
public class ChoiceExample
{
public static void main(String args[])
{
Frame f= new Frame();
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
f.setTitle("ChoiceExample");
Label l=new Label("Country :");
l.setBounds(50,100,75,75);
Choice c=new Choice();
c.setBounds(120,125,75,75);
c.add("India");
c.add("Japan");
c.add("Austraila");
c.add("U.S.A");
c.add("U.K");
f.add(c);
f.add(l);
//code for close window
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
}
Example: An example for List Component in AWT.
ListExample.java
import java.awt.*;
import java.awt.event.*;
public class ListExample
{
public static void main(String args[])
{
Frame f= new Frame();
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
List l1=new List(5);
l1.setBounds(100,100, 100,75);
l1.add("Item 1");
l1.add("Item 2");
l1.add("Item 3");
l1.add("Item 4");
l1.add("Item 5");
f.add(l1);
//Code for Close window
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
}

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

Example:

In this example, we are creating a menu item that contains sub menu as well.
We use MenuItem and Menu class for creating menu.

import java.awt.*;
class MenuDemo1
{
MenuDemo1()
{
Frame menu_f= new Frame("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 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

Example:

Lets create a panel to add components like: button, textbox etc. the panel
provides a place to add awt components.

import java.awt.*;
public class PanelDemo1{
PanelDemo1()
{
Frame panel_f= new Frame("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();
}
}
Example: An example program for Login page in AWT.
LoginExample.java
import java.awt.*;
import java.awt.event.*;
class LoginExample
{
public static void main(String args[])
{
Frame f= new Frame ("Login Page");
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
Label l1,l2;
TextField t1,t2;
Checkbox cb;
Button b;
l1=new Label("User Name :");
l1.setBounds(50,60,100,30);
t1=new TextField("");
t1.setBounds(150,60, 200,30);
l2=new Label("Password :");
l2.setBounds(50,120,100,30);
t2=new TextField("");
t2.setBounds(150,120, 200,30);
cb=new Checkbox("Save Password");
cb.setBounds(50,150,200,30);
b=new Button("Login");
b.setBounds(150,180,100,30);
f.add(l1); f.add(l2);
f.add(t1); f.add(t2);
f.add(cb);
f.add(b);
}
}
Event Handling
Event: Changing the state of an object (component) is known as an event. For
example, click on button, dragging mouse etc.
Event describes the change in state of component. Events are generated as result
of user interaction with the graphical user interface components. For example,
clicking on a button, moving the mouse, entering a character through keyboard
and selecting an item from list.
Def: Event Handling is the mechanism that controls the event and decides what
should happen if an event occurs.
This mechanism has the code which is known as event handler that is executed
when an event occurs.
The java.awt.event package provides many event classes and Listener interfaces
for event handling.
--Steps to perform Event Handling
Following steps are required to perform event handling:
– Register the component with the Listener.
By using addActionListener(ActionListener a)
Example:
Button b=new Button(“Submit”);
b.setBounds(100,50,80,30);
b.addActionListener(this);
– Provide or put event handling code.
By using actionPerformed(ActionEvent e) method of ActionListener Interface,
we can perfom action what the user want.
Example:
Public void actionPerformed(ActionEvent e)
{
tf.setText(“welcome”);
}
Simple Example:
import java.awt.*;
import java.awt.event.*;
class EventExample extends Frame implements ActionListener{
TextField tf;
EventExample(){
tf=new TextField(); //create components
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
b.addActionListener(this);//register listener &passing current instance
add(b);add(tf); //add components and set size, layout and visibility
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
tf.setText("Welcome");
}
public static void main(String args[]){
new EventExample();
}}

You might also like