Unit 3: Introducing AWT

5th Semester

3.1 AWT classes

AWT contains large number of classes and methods that allows you to create and manage graphical user interface ( GUI ) applications, such as windows, buttons, scroll bars,etc.  2 packages – java.awt and java.awt.event – are commonly-used.

  1. The java.awt package contains the core AWT graphics classes:
    • GUI Component classes, such as ButtonTextField, and Label.
    • GUI Container classes, such as Frame and Panel.
    • Layout managers, such as FlowLayoutBorderLayout and GridLayout.
    • Custom graphics classes, such as GraphicsColor and Font.
  2. The java.awt.event package supports event handling:
    • Event classes, such as ActionEventMouseEventKeyEvent and WindowEvent,
    • Event Listener Interfaces, such as ActionListenerMouseListener, MouseMotionListenerKeyListener and WindowListener,
    • Event Listener Adapter classes, such as MouseAdapterKeyAdapter, and WindowAdapter.

 

AWT features include:

  • A rich set of user interface components
  • A robust event handling model
  • Graphics and imaging tools, including space, Color and font classes.
  • Layout managers, for flexible window layouts that don’t depend on a particular window size or screen resolution.
  • Data transfer classes, for cut and paste through the native platform clipboard

Let’s Know in Detail

As you can observe from the above hierarchy, Component is the super class of all Java components and is declared as abstract. That is, we cannot create objects of Component class directly.

  1. Component :- A Component object represents a graphical interactive area displayable on the screen that can be used by the user.Any subclass of a Component class is known as a component. For example, button is a component.
  2. Container :- The Container is a component in AWT that can contain another components like buttons, textfields, labels etc.The commonly-used top-level containers in AWT are Frame, Dialog and Applet.
  3. A component must be kept in a container. You need to identify a container to hold the components. Every container has a method called add(Component c). A container (say c) can invoke c.add(aComponent) to add aComponent into itself. For example,
  4. Window:- The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window.
  5.  Frame :- It  provides the “main window” for your GUI application. It has a title bar (containing an icon, a title, the minimize, maximize/restore-down and close buttons), an optional menu bar, and the content display area.Creating a Frame There are two ways to create a Frame. They are,
    1. By extending Frame class (inheritance)
    2. By creating the object of Frame class (association)
1. By extending Frame class (inheritance)

import java.awt.*;
import java.awt.event.*;
public class Demoextends Frame
{
  public Demo()
 {
  Button btn=new Button("Hello World");
  add(btn); 		//adding a new Button.
  setSize(400, 500);        //setting size.
  setTitle("StudyTonight");  //setting title.
  setLayout(new FlowLayout());	 //set default layout for frame.
  setVisible(true);           //set frame visibilty true.
 }
 public static void main (String[] args)
 {
  Demota = new Demo();   //creating a frame.
 }
}

2. By creating the object of Frame class (association)

import java.awt.*;
public class Demo
{
 Demo()
 {
  Frame fm=new Frame();         //Creating a frame.
  Label lb = new Label("welcome to java graphics");     //Creating a label
  fm.add(lb);		              //adding label to the frame.
  fm.setSize(300, 300);               //setting frame size.
  fm.setVisible(true);                //set frame visibilty true.
 }
 public static void main(String args[])
 {
  Testawt ta = new Testawt();
 }