Saturday, 13 May 2017

java AWT ~ GNIITHELP

AWT contains large number of classes and methods that allows you to create and manage windows GUI application. AWT is the foundation upon which Swing is made. 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.

AWT Hierarchy

heirarchy of component class

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.

Container

Container is a component in AWT that contains another component like button, text field, tables etc. Container is a subclass of component class. Conatiner class keeps track of components that are added to another component.

Panel

Panel class is concrete sub class of Container. Panel does not contain title bar, menu bar or border.

Window class

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

Frame

Frame is a sub class of Window and have resizing canvas. It is a container that contain several different components like button, title bar, textfield, label etc. In Java, most of the AWT applications are created using Frame window. Frame class has two different constructors,
Frame() throws HeadlessException 

Frame(String title) throws HeadlessException 

Creating a Frame

There are two ways to create a Frame. They are,
  1. By Instantiating Frame class
  2. By extending Frame class

Creating Frame Window by Instantiating Frame class

import java.awt.*;
public class Testawt 
{
 Testawt()
 {
  Frame fm=new Frame();         //Creating a frame.
  Label lb = new Label("welcome to java graphics");     //Creating a label
  fm.add(lb);                //adding label to the frame.
  fm.setSize(300, 300);               //setting frame size.
  fm.setVisible(true);                //set frame visibilty true.
 }
 public static void main(String args[])
 {
  Testawt ta = new Testawt(); 
 }
}
creating Frame Window

Creating Frame window by extending Frame class

package testawt;

import java.awt.*;
import java.awt.event.*;

public class Testawt extends Frame 
{
  public Testawt() 
 {
  
  Button btn=new Button("Hello World");  
  add(btn);   //adding a new Button.
  setSize(400, 500);        //setting size.
  setTitle("StudyTonight");  //setting title.
  setLayout(new FlowLayout());  //set default layout for frame.    
  setVisible(true);           //set frame visibilty true.    
  
 }
 
 public static void main (String[] args)
 {
  Testawt ta = new Testawt();   //creating a frame.
 }
}

No comments:

Post a Comment