Introducing the AWT classes

Filter Course


 Introducing the AWT classes

Published by: Nuru

Published date: 02 Mar 2022

 Introducing the AWT classes, Reference notes of BCIS Fifth semester

 AWT classes

AWT stands for Abstract Window Toolkit. It refers to an API to develop Graphical User Interface (GUI) or windows-based applications in Java. 

AWT is platform-independent because of:

Java AWT calls the native platform (operating systems) subroutine for creating API components like TextField, checkbox, button, etc. It has different look-and-feel plugins.

For example, an AWT GUI with components like TextField, label, and button will have different look and feel for the different platforms like Windows, MAC OS, and Unix. The reason for this is the platforms have different views for their native components and AWT directly calls the native subroutine that creates those components.

The AWT is roughly broken into three categories:

  • Components
  • Layout Managers
  • Graphics

AWT Hierarchy:

AWT Hierarchy:

 

Window fundamentals

  1.  Component
  2.  Container
  3.  Panel
  4.  Window
  5.  Frame

1. Component:

The component is the superclass of most of the displayable classes defined within the AWT.  Note: it is abstract.
MenuComponent is another class that is similar to Component except it is the superclass for all GUI items which can be displayed within a drop-down menu. The Component class defines data and methods which are relevant to all Components

Attributes of components are defined by using the given methods:

  • setBounds: sets the boundary
  • setSize: sets the size
  • setLocation: sets the location
  • setFont: sets the font
  • setEnabled: dynamically switch a button’s ability to respond to end-user interactions
  • setVisible: sets the visibility
  • setForeground: sets the foreground color
  • setBackground: sets the background color       

2. Container:

The container is a subclass of Component. ( i.e. All containers are themselves, Components) Containers contain components. For a component to be placed on the screen, it must be placed within a Container.

The Container class defined all the data and methods necessary for managing groups of Components:

  • add
  • getComponent
  • getMaximumSize
  • getMinimumSize
  • getPreferredSize
  • remove
  • removeAll

3.  Panel:

When writing a GUI application, the GUI portion can become quite complex. To manage the complexity, GUIs are broken down into groups of components. Each group generally provides a unit of functionality. A Panel is a rectangular Container whose sole purpose is to hold and manage components within a GUI.

Example:

Panel Panel1 = new Panel();
Panel1.add(new Button("Ok"));
Panel1.add(new Button("Cancel"));

Frame frame1 = new Frame("Button Test");
frame1.setSize(100,100);
frame1.setLocation(10,10);
frame1.add(Panel1);

4. Window:

The Window class defines a top-level Window with no Borders or Menu bar. Usually used for application splash screens

5. Frame

Frame defines a top-level Window with Borders and a Menu Bar. Frames are more commonly used than Windows Once defined, a Frame is a Container which can contain Components

Example:

Frame frame1 = new Frame(“Hello World”);
frame1.setSize(100,100);
frame1.setLocation(10,10);
frame1.setVisible(true);

Working with frame Windows:

 a. Setting the Windows dimensions

frame1.setSize(100,100);
frame1.setLocation(10,10);

b. Setting a Windows title

void setTitle(); 

frame1.setTitle("Frame Title");

c. Hiding and showing

setVisible (true) will show the frame window, on the other hand, setVisible (false) 

Also, frame.show() will show and

d. Closing a Frame Windows

frame.dispose() will hide the frame window.

Example:

frame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            frame.dispose();
         }  

Many AWT components have been replaced by Swing components. It is generally not considered a good idea to mix Swing components and AWT components. You have to choose to use one or the other.

There are two ways to create a GUI using Frame in AWT. They are:

 

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

1. Example of creating AWT frame by inheritance:

package Awt;

import java.awt.*;

public class AwtUsingClass{
    public AwtUsingClass()
    //it is a constructor
    {
        // making a frame as an object
        Frame frame = new Frame("Hello Students!");

         // setting frame size, layout and visibility
        frame.setSize(500, 1000);
        frame.setLayout(null);
        frame.setVisible(true);


        // making an object of textField and setting its boundary with bgColor
        TextField textField=new TextField();
        textField.setBounds(30, 400, 150, 70);
        textField.setBackground(Color.yellow);
       

         // making an object of Label and setting its boundary and bgColor
        Label label=new Label("Hope you are well");
        label.setBounds(30,40,200,100);
        label.setBackground(Color.green);
       

        // making an object of Label and setting its boundary and bgColor
        Button button = new Button("Click Me");
        button.setBounds(30, 150, 100, 200);
        button.setBackground(Color.BLACK);
        button.setForeground(Color.white);
   
        // making an object of checkbox
        Checkbox checkbox=new Checkbox("Science");
        checkbox.setBounds(30, 550, 100, 100);

        Checkbox checkbox1=new Checkbox("Maths");
        checkbox1.setBounds(30, 630, 100, 100);

        // adding all the components in the frame
        frame.add(checkbox);
        frame.add(checkbox1);
        frame.add(textField);
        frame.add(label);
        frame.add(button);

    }
    public static void main(String[] args) {
        //creating an instance or object of class
        AwtUsingClass object1 = new AwtUsingClass();
    }
}

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

package Awt;

import java.awt.*;

public class AwtUsingObject{
    public static void main(String [] args){

        // creating object of frame
        Frame frame = new Frame("AWT Frame by Objects");

         //set frame size, setVisible, layout
        frame.setSize(700, 700);
         frame.setVisible(true);
         frame.setLayout(null);

         // creating object of label, textfield, button and setting its boundary respectively
        Label label1 = new Label("username:");
        label1.setBounds(20,20,80,50);
        TextField textField = new TextField();
        textField.setBounds(110,20,80,50);

        Label label2 = new Label("password:");
        label2.setBounds(20, 80, 80, 50);
        TextField textField1 = new TextField();
        textField1.setBounds(110, 80, 80, 50);
        Button button = new Button("submit");
        button.setBounds(50, 150, 100, 50 );

        // adding all components in the frame
        frame.add(label1);
        frame.add(textField);
        frame.add(label2);
        frame.add(textField1);
        frame.add(button);
       }
}
 

Graphics:

It is possible to draw lines and various shapes within a Panel under the AWT. Each Component contains a Graphics object which defines a Graphics Context which can be obtained by a call to getGraphics(). Common methods used in Graphics include:

  • drawLine
  • drawOval
  • drawPolygon
  • drawPolyLine
  • drawRect
  • drawRoundRect
  • drawString
  • draw3DRect
  • fill3DRect
  • fillArc