What is awt in java

What is awt in java

What is awt in java

In this Blog Post, We can study “What is awt in java”.

The java AWT stands for Abstract Window Toolkit, it is an API which is used to develop a graphical user interface application.

Awt will contain a huge number of classes and methods for creating GUI based applications, like windows, buttons, scroll bars, etc.

Awt (Abstract Window Toolkit) components are always a platform dependant.

For use awt in a program, you need to import package first

Java.awt package

Hierarchy of awt classes.

Component class in awt

This class is on top of awt hierarchy.

This is an abstract class that can encapsulate all attributes of a visual component.

All the GUI elements like text fields, scrollbars, buttons, etc are called components.

In Java Awt we have all classes for a specific component.

Container in awt

The container is a component and it is a platform for containing other components like text fields, buttons, labels, etc.

The extended container classes are Frame, Dialog, and panel it is also one type of container.

It is a subclass of the component class.

Window in awt

Windows instance has no border and menu bars.

It will create a top-level window.

Panel in awt

It is also a container and it will not contain title bars and menu bars or borders.

This is a concrete subclass of Container.

Frame in awt

The Frame is also a container and it contains a title bar and has menu bars.

It is a subclass of window class and has resizing canvas.

Most Useful Method of Component class:

Methods Description
public void add(Component c) It will insert a component.
public void setSize(int width, int height) This method will set the size of the component.
public void setLayout(LayoutManager m) This method will define the component layout manager.
Public void setVisible(Boolean status) It will change component visibility.

Awt Java Example:

There are two ways to create awt example.

  • Using extending Frame class
  • Creating an object of a frame class

By Using extending Frame class

import java.awt.*;

class Sample extends Frame
{

Sample()
{

Button b1=new Button(“click this”);

b1.setBounds(30,100,80,30);// set the button position

add(b1);//add the button into frame

setSize(300,300);//it will set the size of frame

setLayout(null);//no layout manager

setVisible(true);//now frame visible, by default not visible

}

public static void main(String args[])
{

Sample f=new Sample();

}

}

Output:

By using creating objects of a frame class.

import java.awt.*;

class Sample2

{

Sample2()

{

Frame f1=new Frame();

Button b1=new Button(“click this”);

b1.setBounds(30,50,80,30);

f1.add(b1);

f1.setSize(300,300);

f1.setLayout(null);

f1.setVisible(true);

}

public static void main(String args[])
{

Sample2 f=new Sample2();

}

}

Output:

 

Also Read This:

Type Casting in Java

Java Program For Sort Array In Ascending and Descending Order

Java Program To Implement Multilevel Inheritance

Harsh

Hey Friends, Welcome to thetechbuddy. This blog is about technical or programming knowledge.

Leave a Reply