In this article we will look at a brief introduction to AWT which is a package used for creating GUIs and is a foundation for more advanced classes.
AWT (Abstract Window Toolkit) was Java’s first GUI framework, which was introduced in Java 1.0. It is used to create GUIs (Graphical User Interfaces). Although programmers use more advanced frameworks like Swings and JavaFX, it is important to know that they are built on top of AWT.
AWT Classes
The AWT framework contains many classes and interfaces using which we can create GUIs. The AWT package is java.awt. Some of the frequently used AWT classes are listed below:
Various classes in java.awt package are arranged in a hierarchy as shown below:
Component and Container
The Component is the abstract root class for many GUI control classes. Container is a sub class of Component class. The Component and various Container classes are arranged in a hierarchy as shown below:
Component
Component is the abstract class that encapsulates all the properties of a visual component. Except for menus, most of the GUI components are inherited from the Component class.
Container
Container is a sub class of the Component class which can be used to hold other components. A Container object can hold other Containers also. A Container is responsible for laying out (positioning) the components.
Panel
Panel class is a concrete sub class of the Container class. A Panel object is a window without title bar, menu bar and border. Panel is the super class of Applet class and is capable of holding other components or containers.
Window
Window is a sub class of Container class. A Window creates a top-level container which can hold other components or containers.
Frame
Frame is a concrete sub class of Window class. The Frame encapsulates a window. Frame contains a title bar, menu bar, borders and resizable corners. To create stand alone applications in Java, we generally use Frame.
Canvas
Canvas class is derived from the Component class. A Canvas encapsulates a blank window on which we can draw.
Frame Class
The Frame class is used to create standard windows. Following are two Frame class constructors:
Frame()
Frame(String title)
Following are some of the frequently used methods of Frame class:
void setSize(int width, int height) – Used to specify the width and height of the frame window.
void setSize(Dimension reference) – Used to specify the dimensions of the frame window.
Dimension getSize() – Returns the dimensions of the frame window.
void setVisible(boolean visibleFlag) – Makes the frame window visible or non-visible based on the boolean parameter.
void setTitle(String title) – Used to set the title of the frame window.
Following is Java code for creating a simple frame window:
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame
{
MyFrame()
{
setSize(600, 300);
setTitle("My Application");
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
}
public void paint(Graphics g)
{
g.drawString("This is a frame!", 40, 80);
}
public static void main(String[] args)
{
MyFrame mf = new MyFrame();
}
}
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply