Java AWT

Java AWT stands for Abstract Window Toolkit. It is Java API that is used to create Graphical User Interface GUI in platform independent environment. Java AWT is used to create window based applications in Java language. Java AWT components are platform-dependent that means the components are displayed according to the view of OS. AWT is heavyweight which means its components are using the resources of the operating system.

The package name is java.awt that can be downloaded from the official Java website. It includes classes for AWT API such as TextField, Label, TextArea, RadioButton, Checkbox, Choice, List, etc.

Container

Container is a main component of AWT. It contains other components such as buttons, text fields, labels etc. The classes used in GUI java programming extend from Container class for example Frame, Dialog and Panel.

Window

Window class extends from Container. It is a container that has no border or menu bars. A window must have a frame, dialog or another window.

Panel

Panel is the type of container that has no menu bar or title bar. It can have other components such as button, text field etc.

Frame

Frame is a subclass of Window. Frame is the container that has a title bar. It can further contain menu bar and other components like button, text fields, etc. Frame is used in making most java AWT applications. Frame class has two constructors. The two constructors are given below:

  • Frame() throws Headless Exception
  • Frame(String title) throws Headless Exception

Components Class

The component class is an abstract class containing the properties all components visible on the screen. It contains multiple useful methods that are used in GUI java programming. The table below contains the list of methods and their descriptions. Frames are used to create java awt examples. These methods are used to create frames.

Sr. No.MethodDescription
1.public void add(Component c)This method adds a component on a component.
2.public void setSize(int width,int height)This method sets the height and width (size) of the component.
3.public void setLayout(LayoutManager m)This method sets the layout manager for the component.
4.public void setVisible(boolean status)This method sets the visibility of component to the default size.
Methods of component class

The below example shows how to use java awt in code. In this example, the programmer uses the method of inheritance to extend the Frame class. The first step is to create a frame. The frame is further used to create a labelled button which can perform an action.

// The file name is Example.java
import java.awt.*;  
class Example extends Frame{  
Example(){  
Button b=new Button("Button");  
b.setBounds(30,100,80,30);// setting button position  
add(b);//adding button into frame  
setSize(300,300);//setting frame size 300 width and 300 height  
setLayout(null);// setting layout manager to null
setVisible(true);//now frame will be visible, by default not visible  
}  
public static void main(String args[]){  
Example f=new Example();  
}
}  

In this code, the programmer creates a component that is a button. The Example class extends from Frame class.This button exits inside the dimensions of the frame. The setBounds(int x, int y, int z, int a) is used to set the position of awt button. The methods setSize(int width, int height) and setVisibility(true) are used to create visibility of frame. The next step is to add the component that is a button into the frame using add() method. The output of the above code after compilation is:

Screenshot 515
Java AWT