You are on page 1of 37

AWT- Abstract Window Toolkit

Mr.P.M.Sawant.
Dept. Of Computer Science
Prof.Ramkirshna More ACS
College,akurdi
Email Id:sawanprasad@gmail.Com.
AWT- Abstract Window Toolkit

• The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT
features include:
• A set of native user interface components
• A robust event-handling model
• Graphics and imaging tools, including shape, color, and font classes
• Layout managers, for flexible window layouts that do not depend on a particular window size or
screen resolution
• Data transfer classes, for cut-and-paste through the native platform clipboard
Frame
Frame

 Most common way is to use the single argument constructor


 The argument is a String that becomes the window’s title
 The window is initially invisible, you must call the Frame’s show() method which it
inherits from the window class
Constructor
Frame ()
Create invisible Frame

Frame(String title)
Create invisible Frame along with title
Frame Demo

import java.awt.*;
import java.awt.event.*;
import sun.awt.WindowClosingListener;
public class FrameDemo extends Frame
{
public FrameDemo()
{
Frame frame = new Frame("Welcom in RMACS");
Label lbl = new Label("Welcom in RMACS",Label.CENTER);
frame.add(lbl);
frame.setSize(400,400);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0); } });
}
public static void main(String[] args)
{
new FrameDemo();
}
}
Label
Label

Constructor

Label()
Create blank Label

Label(String str)
Create Label that contains the string specified by str

Label(String str, int how )


Create Label that contains the string specified by str using alignment specified by how .
LabelDemo
import java.awt.*;
import java.awt.event.*;
import sun.awt.WindowClosingListener;
public class LabelDemo extends Frame
{
public LabelDemo()
{
Frame frame = new Frame("Welcom in RMACS");
Label lbl1 = new Label("TY B.Sc(Computer Sci.)",Label.CENTER);
frame.add(lbl1);
frame.setSize(400,400);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we){
System.exit(0);
} });
}
public static void main(String[] args)
{
new LabelDemo();
}
Button
Button

Constructor

Button()
Create Push button

Button( String str)


Create Push button having caption str
ButtonDemo
import java.awt.*;
import java.awt.event.*;
import sun.awt.WindowClosingListener;
public class ButtonDemo extends Frame
{
public ButtonDemo()
{
Frame frame = new Frame("Welcom in RMACS");
Label lbl1 = new Label("TY B.Sc(Computer Sci.)",Label.CENTER);
Button btn1=new Button("Press");
Button btn2=new Button("Cancel");
frame.setLayout(new GridLayout(0,3));
frame.add(lbl1);
frame.add(btn1);
frame.add(btn2);
frame.setSize(400,100);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0); } });
}
public static void main(String[] args)
{ new ButtonDemo(); }
}
Checkbox
Checkbox

Constructor

Checkbox()
Creates a check box with no label.
Checkbox (String label)
Creates a check box with the specified label.
Checkbox (String label, boolean state)
Creates a check box with the specified label and sets the specified state.
Checkbox (String label, boolean state, CheckboxGroup group)
Constructs a Checkbox with the specified label, set to the specified state, and in the
specified check box group.
Checkbox (String label, CheckboxGroup group, boolean state)
Creates a check box with the specified label, in the specified check box group, and set to the
specified state.
CheckboxDemo

import java.awt.*;
import java.awt.event.*;
import sun.awt.WindowClosingListener;
public class CheckboxDemo extends Frame
{
public CheckboxDemo()
{
Frame frame = new Frame("Welcom in RMACS");
Checkbox c1=new Checkbox("Window 98/XP",null,true);
Checkbox c2=new Checkbox("Window Vista");
Checkbox c3=new Checkbox("Linux");
frame.setLayout(new GridLayout(0,3));
frame.add(c1);
frame.add(c2);
frame.add(c3);
frame.setSize(400,100);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0); } });
}
public static void main(String[] args)
{ new CheckboxDemo(); }
}
CheckboxGroup
import java.awt.*;
import java.awt.event.*;
import sun.awt.WindowClosingListener;
public class RedioButtonDemo extends Frame
{
public RedioButtonDemo()
{
Frame frame = new Frame("Welcom in RMACS");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox cb1=new Checkbox("java",cbg,true);
Checkbox cb2=new Checkbox(".net",cbg,true);
Checkbox cb3=new Checkbox("php",cbg,true);
frame.setLayout(new GridLayout(0,3));
frame.add(cb1);
frame.add(cb2);
frame.add(cb3);
frame.setSize(400,100);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){ System.exit(0); } });
}
public static void main(String[] args)
{new RedioButtonDemo(); }
}
Text Filed
Text Filed

Constructor

TextField ()
Constructs a new text field.
TextField(int columns)
Constructs a new empty text field with the specified number of columns.
TextField (String text)
Constructs a new text field initialized with the specified text.
TextField (String text, int columns)
Constructs a new text field initialized with the specified text to be displayed, and wide
enough to hold the specified number of columns.
TextFieldDemo
import java.awt.*;
import java.awt.event.*;
import sun.awt.WindowClosingListener;

public class TextFieldDemo extends Frame


{
public TextFieldDemo()
{
Frame frame = new Frame("Welcom in RMACS");
TextField tf1, tf2, tf3, tf4;
tf1 = new TextField();
tf2 = new TextField("", 20);
tf3 = new TextField("Hello!");
tf4 = new TextField("Hello", 30);
frame.setLayout(new GridLayout(2,2));
frame.add(tf1);
frame.add(tf2);
frame.add(tf3);
frame.add(tf4);
frame.setSize(500,100);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0); } });
}
public static void main(String[] args)
{new TextFieldDemo(); }
}
TextArea
Constructor

TextArea()
Constructs a new text area with the empty string as text.
TextArea(int rows, int columns)
Constructs a new text area with the specified number of rows and columns and the empty string as text.
TextArea(String text)
Constructs a new text area with the specified text.
TextArea(String text, int rows, int columns)
Constructs a new text area with the specified text, and with the specified number of rows and columns.

TextArea(String text, int rows, int columns, int scrollbars)


Constructs a new text area with the specified text, and with the rows, columns, and scroll bar visibility
as specified.
TextAreaDemo
import java.awt.*;
import java.awt.event.*;
import sun.awt.WindowClosingListener;
public class TextAreaDemo extends Frame
{
public TextAreaDemo()
{
Frame frame = new Frame("Welcom in RMACS");
TextArea tf1;
tf1 = new TextArea("Hello", 5, 100);
frame.setLayout(new GridLayout(0,1));
frame.add(tf1);
frame.setSize(500,100);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0); } });
}
public static void main(String[] args)
{ new TextAreaDemo();}
}
List

Constructor
List()
Creates a new scrolling list
List (int rows)
Creates a new scrolling list initialized with the specified number of visible lines.
List (int rows, boolean multipleMode)
Creates a new scrolling list initialized to display the specified number of rows.
ListDemo

public ListDemo()
{
Frame frame = new Frame("Welcom in RMACS");
List lst = new List(4, false);
lst.add("Mercury");
lst.add("Venus");
lst.add("Earth");
Label lbl=new
lbl=new Label("List");
frame.setLayout(new
frame.setLayout(new GridLayout(0,2));
frame.add(lbl);
frame.add(lst);
frame.setSize(500,100);
frame.setVisible(true);
frame.setVisible(true);
}
Layout Managers

• Layout To add components to a Frame instance, you need to specify the type of
component layout scheme to be used. A Layout Manager is used to specify to a
container how components within that container should be arranged. The next few
sections explain and demonstrate the various layout managers included in the AWT.

1. FlowLayout
2. BorderLayout
3. Grid ayout
4. GridBagLayout
FlowLayout

The FlowLayout manager is the simplest of the layout managers. In this scheme,
components are arranged in the order they are placed within the container. If a row
becomes completely filled, the remaining components are grouped together on the
next row.
FlowLayoutExample
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FlowLayoutExample extends Frame
{
public FlowLayoutExample(String title, int count)
{
super(title);
setLayout(new FlowLayout());
for(int i=1; i <= count; i++)
add(new Button(Integer.toString(i)));
}
public static void main(String[] args)
{
int count = 4;
FlowLayoutExample f = new FlowLayoutExample("FlowLayoutExample", count);
f.setSize(300, 200);
f.show();
}
}

BorderLayout
The BorderLayout manager arranges components within specified regions of a
container. Valid regions are "North", "South", "East", "West", and "Center".
BorderLayoutExample
GridLayout
• A container using the GridLayout scheme arranges components in rows and columns in
row-major order. Each component is sized to fit its respective grid cell.
ButtonDemo GridLayout
import java.awt.*;
import java.awt.event.*;
import sun.awt.WindowClosingListener;
public class ButtonDemo extends Frame
{
public ButtonDemo()
{
Frame frame = new Frame("Welcom in RMACS");
Label lbl1 = new Label("TY B.Sc(Computer Sci.)",Label.CENTER);
Button btn1=new Button("Press");
Button btn2=new Button("Cancel");
frame.setLayout(new GridLayout(0,3));
frame.add(lbl1);
frame.add(btn1);
frame.add(btn2);
frame.setSize(400,100);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0); } });
}
public static void main(String[] args)
{ new ButtonDemo(); }
}
Event Handling

The event model, which you saw at its simplest in the preceding example, is quite powerful
and flexible. Any number of event listener objects can listen for all kinds of events from any
number of event source objects. For example, a program might create one listener per event
source. Or a program might have a single listener for all events from all sources. A program
can even have more than one listener for a single kind of event from a single event source.

Multiple listeners can register to be notified of events of a particular type from a particular
source. Also, the same listener can listen to notifications from different objects.
Inner Classes and Anonymous Inner Classes

What if you want to use an adapter class, but do not want your public class to inherit from an adapter
class? Inner classes can also be useful for event listeners that implement one or more interfaces
directly.

frame.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent we)
{ System.exit(0);
System.exit(0);
}
}
);
Syntax
Class <classname> extends <container> implements interface

Example
class ButtonDemo extends Frame implmenst ActionListener

btn.addActionListener(this);
Event Class Description Interface
ActionEvent When a button is press a list is double ActionListener
clicked or a menu is selected .

AdjustmentEven When a scrollbar is used AdustmentListener


t
ComponentEvenWhen a component is resized, ComponetListener
t moved,hidden,or made visible.
FocusEvent When a component loses or gain FocusListener
keyboard focus.
ItemEvent When a menu item is selected or ItemListener
deselected or when a checkbox or list
item is clicked.
WindowEvent When a window is WindowListener
activated,close,open,or quit.
MouseEvent When mouse is MouseListener,MouseMoveListner
moved,clicked,dragged or released .
KeyEvent When input is received from the KeyListener
keyborad
Question
• :
Write a JAVA program for following screen