Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

JAVA AWT Introduction

Java AWT (Abstract Windowing Toolkit) is an API which contains large number of classes and methods used to create and manage windows GUI application.

AWT is the basic fundamentals upon which Swing have developed. It is used for GUI programming in Java. But now a days it is merely used because most GUI java programs are implemented using Swing because of its rich implementation of GUI controls and light-weighted nature.

Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e. its components uses the resources of system because they are being generated by underlying operating system (OS). For example if you are instantiating a text box in AWT that means you are actually asking OS to create a text box for you.


JAVA AWT Hierarchy

The Java AWT hierarchy classes are given below.

awt hirerchy

Component Class:

Component class is at the top of AWT hierarchy.
Component is an abstract class that encapsulates all attribute of visual component.
A component object is responsible for remembering the current foreground and background colours and the currently selected text font. All the elements like buttons, text fields, scrollbars etc are known as components.

Container:

The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. Container is a subclass of component class.


Panel:

The Panel is the container that doesn't contain title bar and menu bars. It can have other components like button, textfield etc.


Window Class:

Window class creates a top level window. Window does not have borders and menu bar.


Frame:

The Frame is a sub class of Window .The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc.


Java AWT Example:

To develop a GUI, we need a frame. There are two ways to create a frame in AWT.

  • • By extending Frame class
  • • By creating the object of Frame class
AWT Example 1: creating Frame by extending Frame class
import java.awt.*;
public class FirstExample extends Frame
{
	FirstExample()
	{ 
		Button b=new Button("OK");
		// setting button position on screen
		b.setBounds(50,50,50,50); 
												
		//adding button into frame
		add(b);
												
		//Setting Frame width and height
		setSize(500,300); 
												
		//Setting the title of Frame
		setTitle("My First AWT example"); 
												
		//Setting the layout for the Frame
		setLayout(new FlowLayout());
												
		/* By default frame is not visible so
		* we are setting the visibility to true
		* to make it visible.
		*/
		setVisible(true);
	}
	public static void main(String args[])
	{
		// Creating the instance of Frame
		FirstExample f=new FirstExample();
	}
}

Output:

output

AWT Example 2: creating Frame by creating instance of Frame class
import java.awt.*;
public class SecondExample
{
	SecondExample2()
	{
		//Creating Frame
		Frame f=new Frame();    
											  
		//Creating a label
		Label lb = new Label("UserName: ");
											  
		//adding label to the frame
		fr.add(lb);          
											  
		//Creating Text Field
		TextField t = new TextField();
											  
		//adding text field to the frame
		fr.add(t);
											  
		//setting frame size
		f.setSize(500, 300);
											  
		//Setting the layout for the Frame
		f.setLayout(new FlowLayout());
													
		f.setVisible(true);          
	}
	public static void main(String args[])
	{
		SecondExample se = new SecondExample();
	}
}
Output:

output2

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext