Java AWT Basic Concept


Java AWT :

Java AWT for Abstract windows toolkit. The AWT is part of the Java Foundation Classes (JFC) the standers API for providing Graphical user Interfaces(GULs)for java programs. 

The AWT contains numerous classes and methods that allow you to create and mange windows. Although the main Purpose of the AWT is to support applet windows, it can also be used to create stand-alone windows that run in a GUI environment, such as windows. 


Java AWT Hierarchy :


Container :

The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The classes that extends Container class are known as container such as Frame, Dialog and Panel.

Methods :
  • void Add(Component c)
  • void remove(Component c)
  • void setLayout(LayoutManager m)
  • void validate()
  • int getComponentCount()
  • component getComponentAt(int x,int y)


Window :

The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window.

Methods :
  • Windows(Frame owner)
  • Windows(Windows owner)


Panel :

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

Frame :

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

Useful Methods of Component class :

MethodDescription
public void add(Component c)inserts a component on this component.
public void setSize(int width,int height)sets the size (width and height) of the component.
public void setLayout(LayoutManager m)defines the layout manager for the component.
public void setVisible(boolean status)changes the visibility of the component, by default false.

Java AWT Example :

To create simple awt example, you need a frame. There are two ways to create a frame in AWT.
  • By extending Frame class (inheritance)
  • By creating the object of Frame class (association)

Simple example of AWT by inheritance :

  1. import java.awt.*;  
  2. class First extends Frame{  
  3. First(){  
  4. Button b=new Button("click me");  
  5. b.setBounds(30,100,80,30);// setting button position  
  6.   
  7. add(b);//adding button into frame  
  8. setSize(300,300);//frame size 300 width and 300 height  
  9. setLayout(null);//no layout manager  
  10. setVisible(true);//now frame will be visible, by default not visible  
  11. }  
  12. public static void main(String args[]){  
  13. First f=new First();  
  14. }}  



0 Comments:

Post a Comment