ABSTRACT WINDOW TOOLKIT (AWT)



  • AWT means Abstract Window Toolkit
  • AWT Consists of some set of classes to create GUI (GRAPHICAL USER INTERFACE) Components.
  • The Components present in java.awt package are:
  1. Button
  2. Checkbox
  3. RadioButton
  4. Choice
  5. List
  6. Label
  7. TextField
  8. TextArea
  9. Scrollbar
  • We can create our components either in Applet or in Frame.

BUTTON: A button triggers a series of events.

PROGRAM:- To display the button in applet

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

public class Buttondemo extends Applet implements ActionListener
{
   Button b1,b2;

   public void init()
   {
      setLayout(new FlowLayout());
      b1=new Button("New User Registration");
      b2=new Button("Sign In");
     add(b1);
     add(b2);
     b1.addActionListener(this);
     b2.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae)
    {
       String str=ae.getActionCommand();
       if(str.equals("New User Registration"))
          System.out.println(b1.getLabel());
       if(str.equals("Sign In"))
           System.out.println(b2.getLabel());
     }
}

/*
*/

File Name: -    Buttondemo.java    
Commands:-
                             javac    Buttondemo.java
        appletviewer Buttondemo.java    
OUTPUT:-    
                       Buttons are displayed in applet
RESULT:-
                      Click the button then the label is displayed in command prompt




PROGRAM:- To display the button in frame

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

class Buttondemo1 extends Frame implements ActionListener
{
   Button b1,b2,b3;

   Buttondemo1()
   {
       FlowLayout flow=new FlowLayout(FlowLayout.LEFT);

       setLayout(flow);

       System.out.println(flow.getHgap());
       System.out.println(flow.getVgap());

       flow.setHgap(30);
       flow.setVgap(50);

      b1=new Button("Yellow");
      b2=new Button("Blue");
      b3=new Button();

      b3.setLabel("Green");

      b1.setBackground(Color.blue);
      b1.setForeground(Color.white);

      Font f=new Font("Arial",Font.BOLD|Font.ITALIC,30);

      b1.setFont(f);

      add(b1);
      add(b2);
      add(b3);

      b1.addActionListener(this);
      b2.addActionListener(this);
      b3.addActionListener(this);
     }

    public void actionPerformed(ActionEvent ae)
    {
      String str=ae.getActionCommand();

      if(str.equals("Yellow"))
         setBackground(Color.yellow);

      if(str.equals("Blue"))
         setBackground(Color.blue);

      if(str.equals("Green"))
         setBackground(Color.green);
     }

     public static void main(String args[])
    {
       Buttondemo1 d=new Buttondemo1();
       d.setTitle("Buttondemo1");
       d.setSize(300,300);
       d.setVisible(true);
     }
}

File Name: -    Buttondemo1.java    
Commands:-
                           javac    Buttondemo1.java
      java      Buttondemo1    
OUTPUT:-    
                      Buttons are displayed in frame
RESULT:-  

                      Click the button then frame background color is changed.


PROGRAM:- To display the button in applet using border layout

import java.applet.*;
import java.awt.*;

public class Buttondemo2 extends Applet
{
   Button b1,b2,b3,b4,b5;

   public void init()
   {
      BorderLayout border=new BorderLayout();

      setLayout(border);

      System.out.println(border.getHgap());
      System.out.println(border.getVgap());

      border.setHgap(30);
      border.setVgap(50);

      b1=new Button("This is header");
      b2=new Button("EAST");
      b3=new Button("WEST");
      b4=new Button("This is footer");
      b5=new Button("This is body");

      add(b1,BorderLayout.NORTH);
      add(b2,BorderLayout.EAST);
      add(b3,BorderLayout.WEST);
      add(b4,BorderLayout.SOUTH);
      add(b5,BorderLayout.CENTER);
    }
}

/*
*/

File Name: -    Buttondemo2.java    
Commands:-
                           javac    Buttondemo2.java
      appletviewer Buttondemo2.java    
OUTPUT:-    

                          Buttons are displayed in applet in borders

CHECKBOX: 

       Ø It allows user to select multiple options at a time.

PROGRAM:-  To display the checkboxes

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

class CBdemo extends Frame implements ItemListener
{
   Checkbox c1,c2,c3;
   String msg;

   CBdemo()
   {
      setLayout(new FlowLayout());

      c1=new Checkbox("C",null,true);
      c2=new Checkbox("C++");
      c3=new Checkbox("Java");

      add(c1);
      add(c2);
      add(c3);

     c1.addItemListener(this);
     c2.addItemListener(this);
     c3.addItemListener(this);
     }

     public void itemStateChanged(ItemEvent ie)
    {
        repaint();
     }

     public void paint(Graphics g)
    {
      g.drawString("The state of selected checkboxes are",80,100);

      msg="C:"+c1.getState();
      g.drawString(msg,80,120);

      if(c1.getState()==true)
         g.drawString(c1.getLabel(),150,120);

      msg="C++:"+c2.getState();
      g.drawString(msg,80,140);

      if(c2.getState()==true)
       g.drawString(c2.getLabel(),150,140);

      msg="Java:"+c3.getState();
      g.drawString(msg,80,160);

      if(c3.getState()==true)
         g.drawString(c3.getLabel(),150,160);
    }

    public static void main(String args[])
    {
       CBdemo d=new CBdemo();
       d.setTitle("CBdemo");
       d.setSize(300,300);
       d.setVisible(true);
     }
}

File Name: -    CBdemo.java    
Commands:-
                            javac    CBdemo.java
        java      CBdemo    
OUTPUT:-    
                      checkboxes are displayed

RADIOBUTTON:

         Ø It allows user to select only one option at a time in a group.
                                         

 PROGRAM:-  To display the radiobuttons

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

class RBdemo extends Frame implements ItemListener
{
   CheckboxGroup cbg;
   Checkbox m,f;
   String msg;

   RBdemo()
   {
       setLayout(new FlowLayout());

       cbg=new CheckboxGroup();

       m=new Checkbox("Male",cbg,true);
       f=new Checkbox("Female",cbg,false);

      add(m);
      add(f);

      m.addItemListener(this);
      f.addItemListener(this);
     }

     public void itemStateChanged(ItemEvent ie)
    {
      repaint();
    }

    public void paint(Graphics g)
    {
       g.drawString("The selected radio button is:",80,100);
       msg=cbg.getSelectedCheckbox().getLabel();
       g.drawString(msg,80,120);
    }

    public static void main(String args[])
    {
       RBdemo d=new RBdemo();
       d.setTitle("Rbdemo");
       d.setSize(300,300);
       d.setVisible(true);
     }
}

File Name: -    RBdemo.java    
Commands:-
                            javac    RBdemo.java
        java      RBdemo
OUTPUT:-    
                      RadioButtons are displayed

CHOICE:
       Ø It allows user to select only one item at a time.

PROGRAM:-  To display the choice

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

class Choicedemo extends Frame implements ItemListener
{
   Choice ch;
   String msg;

   Choicedemo()
   {
      setLayout(new FlowLayout());

      ch=new Choice();

      ch.addItem("C");
      ch.addItem("C++");
      ch.addItem("Java");
      ch.addItem("Oracle");

      add(ch);

      ch.addItemListener(this);
     }

    public void itemStateChanged(ItemEvent ie)
    {
       repaint();
    }

    public void paint(Graphics g)
    {
      g.drawString("The selected item is:",80,100);
      msg=ch.getSelectedItem();
      g.drawString(msg,80,120);
    }

    public static void main(String args[])
    {
       Choicedemo d=new Choicedemo();
       d.setTitle("Choicedemo");
       d.setSize(300,300);
       d.setVisible(true);
    }
}

File Name: -    Choicedemo.java    
Commands:-
                            javac    Choicedemo.java
       java      Choicedemo  
OUTPUT:-    
                           Choice are displayed

PROGRAM:-  To display the multiple choices

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

class Choicedemo1 extends Frame implements ItemListener
{
   Choice ch1,ch2;
   String msg;

   Choicedemo1()
   {
      setLayout(new FlowLayout());

      ch1=new Choice();

      ch1.addItem("--Select City--");
      ch1.addItem("Hyderabad");
      ch1.addItem("Vijayawada");
      ch1.addItem("Chennai");

      ch2=new Choice();

      ch2.addItem("--Select State--");
      ch2.addItem("AndhraPradesh");
      ch2.addItem("TamilNadu");

      add(ch1);
      add(ch2);

      ch1.addItemListener(this);
      ch2.addItemListener(this);
     }

     public void itemStateChanged(ItemEvent ie)
    {
       msg=ch1.getSelectedItem();

       if(msg.equals("Hyderabad") || msg.equals("Vijayawada"))
       {
          ch2.select("AndhraPradesh");
      }

       if(msg.equals("Chennai"))
      {
         ch2.select("TamilNadu");
       }

      if(msg.equals("--Select City--"))
      {
         ch2.select("--Select State--");
       }
    }

    public static void main(String args[])
    {
       Choicedemo1 d=new Choicedemo1();
       d.setTitle("Choicedemo1");
       d.setSize(300,300);
       d.setVisible(true);
    }
}

File Name: -    Choicedemo1.java    
Commands:-
                            javac    Choicedemo1.java
       java      Choicedemo1
OUTPUT:-    
                           Choice are displayed 

LIST:
     Ø It allows user to select multiple items at a time.

PROGRAM:-  To display the list

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

class Listdemo extends Frame implements ItemListener
{
   List ch;
   String msg[];

   Listdemo()
   {
      setLayout(new FlowLayout());

      ch=new List(3,true);

      ch.add("C");
      ch.add("C++");
      ch.add("Java");
      ch.add("Oracle");

      add(ch);

      ch.addItemListener(this);
    }

    public void itemStateChanged(ItemEvent ie)
    {
       repaint();
    }

    public void paint(Graphics g)
    {
       g.drawString("The selected items are:",80,100);

        msg=ch.getSelectedItems();

        for(int i=0;i
           g.drawString(msg[i],80,120+(i*10));
     }

     public static void main(String args[])
    {
       Listdemo d=new Listdemo();
       d.setTitle("Listdemo");
       d.setSize(300,300);
       d.setVisible(true);
     }
}

File Name: -    Listdemo.java    
Commands:-
                             javac    Listdemo.java
        java      Listdemo
OUTPUT:-    
                             lists are displayed

LABEL:
      Ø It will give information to the user about another component.

TEXTFIELD:
      Ø It allows user to enter single line of text.

PROGRAM:-  To display label and textfield

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

class Textdemo extends Frame implements ActionListener
{
   Label n,p;
   TextField name,pass;

   Textdemo()
    {
       setLayout(new FlowLayout());

       n=new Label("Enter Name:",Label.LEFT);
       p=new Label("Enter Password:",Label.RIGHT);

        name=new TextField(15);
        pass=new TextField(10);

        name.setText("username");

        pass.setEchoChar('*');

       n.setBackground(Color.blue);
       n.setForeground(Color.white);

       name.setBackground(Color.orange);
       name.setForeground(Color.red);

       add(n);
       add(name);
       add(p);
       add(pass);

       name.addActionListener(this);
       pass.addActionListener(this);
      }

      public void actionPerformed(ActionEvent ae)
      {
         repaint();
       }

      public void paint(Graphics g)
     {
        g.drawString("Name is:"+name.getText(),80,100);
     }

     public static void main(String args[])
     {
        Textdemo d=new Textdemo();
        d.setTitle("Textdemo");
        d.setSize(300,300);
        d.setVisible(true);
     }
}

File Name: -    Textemo.java    
Commands:-
                            javac    Textdemo.java
       java      Textdemo
OUTPUT:-    
                           Textfields  are displayed

TEXTAREA:
       Ø It allows user to enter multiple lines of text.

PROGRAM:-  To display the textarea

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

class Textareademo extends Frame implements ActionListener
{
    Label addr;
    TextArea address;
    Button b;

    Textareademo()
    {
      setLayout(new FlowLayout());

      addr=new Label("Enter Address:",Label.LEFT);
      address=new TextArea(1,10);
      b=new Button("Click");

      add(addr);
      add(address);
      add(b);

      b.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae)
   {
     repaint();
   }

   public void paint(Graphics g)
   {
      g.drawString("Address:"+address.getText(),80,200);
   }

   public static void main(String args[])
   {
      Textareademo d=new Textareademo();
      d.setTitle("TAdemo");
      d.setSize(300,300);
      d.setVisible(true);
    }
}

File Name: -    Textemo1.java    
Commands:-
                            javac    Textdemo1.java
       java      Textdemo1
OUTPUT:-    
                           Textarea  are displayed

SCROLLBAR:
        Ø Scrollbars are of two types.

PROGRAM:-  To display the scrollbars

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

class Scrolldemo extends Frame implements AdjustmentListener
{
   Scrollbar sb1,sb2;

   Scrolldemo()
   {
     setLayout(null);

     sb1=new Scrollbar(Scrollbar.VERTICAL,80,50,0,300);
     sb1.setBounds(400,50,30,300);

     sb2=new Scrollbar(Scrollbar.HORIZONTAL,40,5,0,300);
     sb2.setBounds(50,400,300,30);

    add(sb1);
    add(sb2);

    sb1.addAdjustmentListener(this);
    sb2.addAdjustmentListener(this);
   }

   public void adjustmentValueChanged(AdjustmentEvent ae)
  {
    repaint();
   }

   public void paint(Graphics g)
   {
     g.drawString("ver value:"+sb1.getValue(),80,100);
     g.drawString("hor value:"+sb2.getValue(),80,120);
    g.drawString("ver min value:"+sb1.getMinimum(),80,140);
    g.drawString("hor min value:"+sb2.getMinimum(),80,160);
    g.drawString("ver max value:"+sb1.getMaximum(),80,180);
    g.drawString("hor max value:"+sb2.getMaximum(),80,200);
  }

public static void main(String args[])
{
     Scrolldemo d=new Scrolldemo();
     d.setTitle("Scrolldemo");
     d.setSize(300,300);
     d.setVisible(true);
   }
}

File Name: -    Scrolldemo.java    
Commands:-
                            javac    Scrolldemo.java
       java      Scrolldemo 
OUTPUT:-    
                           scrollbars  are displayed