You are on page 1of 16

AWT- Abstract Window Toolkit

Mr.P.M.Sawant.
Dept. BCA
Prof.Ramkirshna More ACS College,Akurdi
Email Id:sawanprasad@gmail.Com.

© BCA
Prof.Ramkirshna More ACS College,Akurdi
AWT- Abstract Window Toolkit
Frame
Frame Constructor
Frame ()
Create invisible Frame
Frame(String title)
Create invisible Frame along with title

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(); } }

© BCA
Prof.Ramkirshna More ACS College,Akurdi
AWT- Abstract Window Toolkit

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 .

DEMO
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();}}

© BCA
Prof.Ramkirshna More ACS College,Akurdi
AWT- Abstract Window Toolkit
Button

Constructor

Button()
Create Push button
Button( String str)
Create Push button having caption str

DEMO
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(); }
}

© BCA
Prof.Ramkirshna More ACS College,Akurdi
AWT- Abstract Window Toolkit
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.

DEMO
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(); }
}

© BCA
Prof.Ramkirshna More ACS College,Akurdi
AWT- Abstract Window Toolkit

CheckboxGroup

DEMO
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(); }
}

© BCA
Prof.Ramkirshna More ACS College,Akurdi
AWT- Abstract Window Toolkit

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.

DEMO
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();}
}

© BCA
Prof.Ramkirshna More ACS College,Akurdi
AWT- Abstract Window Toolkit

TextField

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.

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(); }
}

© BCA
Prof.Ramkirshna More ACS College,Akurdi
AWT- Abstract Window Toolkit
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.

DEMO
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 Label("List");
frame.setLayout(new GridLayout(0,2));
frame.add(lbl);
frame.add(lst);
frame.setSize(500,100);
frame.setVisible(true);
}

© BCA
Prof.Ramkirshna More ACS College,Akurdi
AWT- Abstract Window Toolkit

Layout Managers
Layout To add components to a Frame instance, you need to specify the type of
componentlayout scheme to be used. A Layout Manager is used to specify to a container how
components within that container should be arranged.

1. FlowLayout
2. BorderLayout
3. GridLayout
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.

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();
}
}

© BCA
Prof.Ramkirshna More ACS College,Akurdi
AWT- Abstract Window Toolkit

BorderLayout

The BorderLayout manager arranges components within specified regions of a container. Valid
regions are "North", "South", "East", "West", and "Center".

DEMO
import java.awt.*;
public class BorderLayoutExample extends Frame
{
public BorderLayoutExample(String title)
{
super(title);
add("North", new Button("North"));
add("South", new Button("South"));
add("East", new Button("East"));
add("West", new Button("West"));
add("Center", new Button("Center"));
}
public static void main(String[] args)
{
BorderLayoutExample ble = new BorderLayoutExample("BorderLayoutExample");
ble.setSize(300, 200);
ble.show();
}
}

© BCA
Prof.Ramkirshna More ACS College,Akurdi
AWT- Abstract Window Toolkit

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.

DEMO
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(); }
}

© BCA
Prof.Ramkirshna More ACS College,Akurdi
AWT- Abstract Window Toolkit

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.

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.

Example
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);

Syntax

Class <classname> extends <container> implements interface

Example

class ButtonDemo extends Frame implmenst ActionListener


btn.addActionListener(this);

© BCA
Prof.Ramkirshna More ACS College,Akurdi
AWT- Abstract Window Toolkit

Event Class Description Interface

ActionEvent When a button is press a list is double ActionListener


clicked or a menu is selected .

AdjustmentEvent When a scrollbar is used AdustmentListener

ComponentEvent When a component is resized, ComponetListener


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 activated,close,open,or WindowListener


quit.

MouseEvent When mouse is moved,clicked,dragged or MouseListener,MouseMoveListner


released .

KeyEvent When input is received from the keyborad KeyListener

Question
1) Write a JAVA program for following screen:

© BCA
Prof.Ramkirshna More ACS College,Akurdi
AWT- Abstract Window Toolkit
Solution

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

/**
* @author Mr.P.M.Sawant.
* Dept. Of Computer Science
* Prof.Ramkirshna More Arts Commerce And Science College,Akurdi
* email id:sawanprasad@gmail.com
*
*/
public class Slip1A extends Frame implements ActionListener
{
Button lowerBtn, upperBtn, boldBtn, italicBtn;
TextField inputText;
Label inputLabel,lowerLabel, upperLabel, boldLabel, italicLabel;
public Slip1A()
{
lowerBtn = new Button("Lower");
upperBtn = new Button("Upper");
boldBtn = new Button("Bold");
italicBtn = new Button("Italic");
inputText = new TextField(20);
inputLabel = new Label("Enter String:",Label.RIGHT);
lowerLabel = new Label("",Label.LEFT);
upperLabel = new Label("",Label.LEFT);
boldLabel = new Label("",Label.LEFT);
italicLabel = new Label("",Label.LEFT);
setLayout(new GridLayout(5,2));
add(inputLabel);
add(inputText);
add(lowerBtn);
add(lowerLabel);
add(upperBtn);
add(upperLabel);
add(boldBtn);
add(boldLabel);
add(italicBtn);
add(italicLabel);
lowerBtn.addActionListener(this);
upperBtn.addActionListener(this);
boldBtn.addActionListener(this);
italicBtn.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setTitle("String Operations");
setSize(200,200);
setVisible(true);
}

© BCA
Prof.Ramkirshna More ACS College,Akurdi
AWT- Abstract Window Toolkit
public void actionPerformed(ActionEvent ae)
{
Button btn = (Button)ae.getSource();
String txt = inputText.getText();
if(btn == lowerBtn)
{
lowerLabel.setText(txt.toLowerCase());
}
if(btn == upperBtn)
{
upperLabel.setText(txt.toUpperCase());
}
if(btn == boldBtn)
{
Font f = new Font("Arial",Font.BOLD,15);
boldLabel.setFont(f);
boldLabel.setText(txt);
}
if(btn == italicBtn)
{
Font f = new Font("Arial",Font.ITALIC,15);
italicLabel.setFont(f);
italicLabel.setText(txt);
}
}

public static void main(String args[])


{
new Slip1A();
}
}

© BCA
Prof.Ramkirshna More ACS College,Akurdi