Table of Contents

Java AWT packages

The Abstract Window Toolkit ( AWT ) package in java enables programmers to create graphical user interface applications. It contains a number of classes that help to implement common windows-based tasks, such as manipulating windows, adding scroll bars, buttons, list items, text boxes, etc. All the classes are contained in the java.awt package

All these classes are hierarchically arranged inside the AWT package in such a manner that each successive level in the hierarchy adds certain attributes to the GUI application.

i) Components:

Component class is the Parent class to all the other classes from which various GUI elements are realized. It is primary responsible for affecting the display of a graphic object on the screen. It also handles the various keyboard and mouse events of the GUI application.

ii) Container:

The Container object contains the other AWT components. It manages the layout and placement of the various AWT components within the container. The Container provides very important Methods to add or Remove the Components from the Applet or from any other Window.

iii) Panel:

It is a subclass of container and it is the super class of Applet. It represents a window space on the application's output is displayed. It is just like a normal window having no border, title bar, menu bar, etc. A Panel is used for displaying Multiple Windows in a Single Window and a Panel may have any Layout.

iv) Window:

The Window is the layout of the window  .Window is also a Sub Class of a Container and window class creates a top level window. These are not directly created, for this the subclass of window name frame is used and dialog are used . This is used for displaying a Sub Windows from a Main Window.

v) Frame:

Frame Class is also a Sub Class of Window Class and frame class allows to Create a pop-Menus. And Frame Class Provides a Special Type of Window which has a title bar, menu bar , border. It supports common window-related events such as close, open, activate, deactivate, etc.

Example:

//Framed.java
import java.awt.*;
public class Framed extends Frame
{
public static void main(String args[])
{
/* Creating a frame object */
               Frame frame=new Frame("My First Frame");
               frame.setSize(400,450);
               frame.setVisible(true);
}
}

output:

// CanvasExample.java
import java.awt.*;  
public class CanvasExample  
{  
 public CanvasExample()  
 {  
   Frame f= new Frame("Canvas Example");  
   f.add(new MyCanvas());  
   f.setSize(400, 400);  
   f.setVisible(true);  
 }  
 public static void main(String args[])  
 {  
   new CanvasExample();  

 }  
}  
class MyCanvas extends Canvas  
{  
       public MyCanvas() {  
       setBackground (Color.yellow);  
       setSize(300, 200);  
    }  
 public void paint(Graphics g)  
 {  
   g.setColor(Color.blue);  
   g.fillRect(10, 10, 150, 100);  
 }  
}
output:

vii) Button:

The Button is one of the most used components in AWT. It is a component primarily for interaction with the user and a user can click on a button to achieve an action which is associated with it.

Example:

//ButtonExample.java
import java.awt.*;  
public class ButtonExample {  
public static void main(String[] args) {  
   Frame f=new Frame("Button Example");  
   Button b=new Button(" I am a Button");  
   b.setBounds(30,50,150,50);  
   f.add(b);  
   f.setSize(400,400);  
   f.setLayout(null);  
   f.setVisible(true);  
}  
}

output:

viii) Label:

Label is a component used for placing text in a container and is the easiest component to use. As a label usually shows only a static String, it doesn't support any way to allow interaction with the user.

Example:

//LabelExample.java
import java.awt.*;  
class LabelExample{  
public static void main(String args[]){  
   Frame f= new Frame("Label Example");  
   Label l1,l2;  
   l1=new Label("First Label");  
   l1.setBounds(10,40, 160,30);  
   l2=new Label("Second Label");  
   l2.setBounds(10,90, 160,30);  
   f.add(l1); f.add(l2);  
   f.setSize(400,400);  
   f.setLayout(null);  
   f.setVisible(true);  
}  
}

output:

ix) Check Boxes:

Check Boxes are components that are used to allow user to select data from a specific set of options only. When a checkbox is checked, we say that its state is turned ON, otherwise it is termed as turned OFF.

Example:

//CheckboxExample.java
import java.awt.*;  
public class CheckboxExample  
{  
    CheckboxExample(){  
      Frame f= new Frame("Checkbox Example");  
       Checkbox checkbox1 = new Checkbox("JAVASCRIPT");  
       checkbox1.setBounds(100,100, 50,50);  
       Checkbox checkbox2 = new Checkbox("JAVA", true);  
       checkbox2.setBounds(100,150, 50,50);  
       f.add(checkbox1);  
       f.add(checkbox2);  
       f.setSize(400,400);  
       f.setLayout(null);  
       f.setVisible(true);  
    }  
public static void main(String args[])  
{  
   new CheckboxExample();  
}  
}

output:

x) Menu Bars and Menus:

A menu comes with a pull-down list of menu items from which user can select one at a time. When a lot of options in different categories exist to be opted by the user, menus are the best choice as they take less space on the frame. A click on the MenuItem generates ActionEvent and is handled by ActionListener.

Example:

//MenuExample.java
import java.awt.*;  
class MenuExample  
{  
    MenuExample(){  
        Frame f= new Frame("Menu and MenuItem Example");  
        MenuBar mb=new MenuBar();  
        Menu menu=new Menu("Menu");  
        Menu submenu=new Menu("Sub Menu");  
        MenuItem i1=new MenuItem("File");  
        MenuItem i2=new MenuItem("Edit");  
        MenuItem i3=new MenuItem("Format");  
        MenuItem i4=new MenuItem("View");  
        MenuItem i5=new MenuItem("Help");  
        menu.add(i1);  
        menu.add(i2);  
        menu.add(i3);  
        submenu.add(i4);  
        submenu.add(i5);  
        menu.add(submenu);  
        mb.add(menu);  
        f.setMenuBar(mb);  
        f.setSize(400,400);  
        f.setLayout(null);  
        f.setVisible(true);  
}  
public static void main(String args[])  
{  
new MenuExample();  
}    
}

Output:

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?