WINDOW FUNDAMENTALS

Java Abstract window tool kit package is used for displaying the data within a GUI Environment. Features of AW T Package are as Followings:
  1. It provides us a set of user interface components including windows buttons text fields scrolling list etc.
  2. It provides us the way to laying out these above components.
  3. It provides to create the events upon these components.
The main purpose for using the AWT is using for all the components displaying on the screen. Awt defines all the windows according to a class hierarchy those are useful at a specific level or we can say arranged according to their functionality.

The most commonly used interface is the panels those are used by applets and those are derived from frame which creates a standard window. The hierarchy of this Awt is:-



As you see this Figure this Hierarchical view display all the classes and also their Sub Classes. All the Classes are Contained as a Multi-Level inheritance , As You see that Component Class contains a Panel Class which again Contains a Applet Class so that in the AWT Package are Stored in the form of Multi-values inheritance.



Component Class

This is the super class which defines all the methods for developing the methods those are related with the components and the methods those are related with the Actual Position of the Control and this also provides the various methods to Setup the Sized, Colors either this may a Background or a Foreground Color. It includes all the components those are displayed on the screen and it also defines various types of public methods for managing events such as mouse and keyboard window etc. the Various Methods those are Contained in the Component Class are :- 
  1. Void SetSize(int Width, int Height) : this Method is used to Set the Size by taking the height and width of a Control for Example height of Textbox and width of Textbox. With this Method we Can set height and width of any Control.
  2. Void SetLocation (int x,int y): Used to Set the location on the Applet and this will take the Coordinates in the Form of x and y.
  3. Void setBounds(int x,int y,int width,int height) : this Method will also set the Acual Loction in the Applet by Specifying the X and y Coordinate and also Specify the Width and Height of the Control.
  4. Void setForeground(Color c) : this Method is used to set the Font Color or Text Color of a Control.
  5. Void setBackground(Color c) : This Method is used to set the Background Color of a Control whether this may a Control or any Window Like an Applet,Panel etc.


Container
It is subclass of components which Inherits the various Properties from that Class and it provides to laying out the component , that are contains by using the various layout managers. Means how the Components will be displayed in the applet and in which Manner the Components will be displayed in the window. or Position of the Controls without setting the X and y Axis is determined by the Layout Manager and. The Container Provides very important Methods to add or Remove the Components from the Applet or from any other Window. The various Methods those are Contained in the Container class are :
  1. Void add (object o) :this Method id used to Add a Control in the Applet of in any Container.
  2. Void remove(object o) : This is used if a user wants to Remove an Control from a Container For Example if a user wants to Remove the Control from a Applet then he may uses this Method.
  3. void removeAll() : This Method doesn't take any argument and this will remove all the Controls from the Container.


Panel
It is a subclass of container and it is the super class of Applet. It is a window that does not have a title bar menu bar or border. Generally a Panel is used for displaying Multiple Windows in a Single Window and a Panel may have any Layout. And for Adding any Control in the Panel we have to use the Add Method and there are also some important Methods those are Provided by the Panel Class those are Explained below :- 
  1. Panel() : This is the Name of Class For Using you have to Create the object first.
  2. Void setLocation : Method is used to set the Location of a control in the Panel.
  3. Void setSize() : This will takes two Arguments and those Arguments will Specify the Width and Height of the Panel.
  4. Void setBounds : This Method is used to set the Bounds of an Control in the Panel For Example X and y Axis and Width and Height of a Control.


Window
Window is also a Sub Class of a Container and window class creates a top level window. These are not directly created, for this the subclass of window name frame is used and dialogs are used . This is used for displaying a Sub Windows from a Main Window and the various Methods of Window are as follows:-
  1. Void Pack() : This Method is used for adjusting the Size of Window according to the number of Controls in the Container or in the Applet or in the other window.
  2. Void show() : This Method is used for displaying a Window.
  3. Void dispose() : This Method is used for de-allocate the Memory Space which is Consumed by the Controls of the Window.

Working With Frames

Frame Class is also a Sub Class of Window Class and frame class allows to Create a pop-Menus. And Frame Class Provides a Special Type of Window which has a title bar, menu bar , border. Resizing corners and it is a subclass of window class when we creates A frame object within a applet , will display a warning message that a frame window is created by an applet not by any software. But when a frame window is created by a program other than an applet then a normal window is created. The Various Methods those are Provided by the Frame Class are as follows :- 
  1. Frame() : This is name of Class and used for Creating an object of this Class and this will not display any title on the title bar of Frame.
  2. Frame(String Title) : This will also create an object of Frame Class and this will also Display a Title on the Title bar of the Window.
  3. Void setsize() : this is Method of Frame Class which will be Accessed by the object of Frame Class and this will takes two Arguments to set the Size in the Form of width and Height .
  4. Void show() : This is used for displaying or showing a Frame from a Main Window.
  5. Void setBackground(Color c) : This is used to set the Background Color of the frame.
  6. Void setLocation(int x,int y) : This Method will display the frame on the window according to the values of x and y coordinates those are Specified Always Remember that For Displaying a Frame you have to set the Layout and the default Layout of the Frame is Border Layout.


Creating a Frame
An instance of frame can be created using any of the above discussed constructors. You can use the frame objects unique methods as well as its inherited method by specifying the frame object name with the method name. The syntax to invoke a method is:

Frame1.show();

The following program code show how to create a frame window:
++++++++++++++++++++++++++++++++++++++
import java.awt.*;
public class Framed1 extends Frame
{
public static void main(String args[])
{
/* Creating a frame object */
                Frame frmobj=new Frame("My First Frame");
                frmobj.setSize(400,450);
                frmobj.setVisible(true);
}
}
++++++++++++++++++++++++++++++++++++++

The above code shows how to create an empty frame. The java.awt.* package should be included in the java programs to implement the functions of AWT classes in your program. Saved file with Framed1.java and follow below commands:

C:\>jdk1.4\bin>javac Framed1.java
C:\>jdk1.4\bin>java Framed1



Setting Frame Properties
Frame class inherits all the methods of its super class. The inherited methods such as setSize() and setVisible() enable you to set the propertied for a frame window. The dfault size of frame 0,0 in terms of width and height. You can set the size of a frame at any time in your program using the setSize() method. The syntax to implement the setSize() method is:

void setSize(int width, int height)
void setSize(Dimension size)

In the above syntax, you can specify the width and height of frame window as arguments in the setSize() methods. In addition, you can also specify the dimension for frame window by passing an object of the Dimension class. size is the object of the Dimension class that has width and height fields, which set the size of frame in terms of pixels. These are the two ways of setting frame window size property. You can use the getSize() method to obtain the current size of the frame. The syntax obtain the current size of the frame window is:

Dimension getSize()

This method returns an object of dimension type. This object has width and height fields that store the current size of the frame window, which is displayed on the screen.

Specifying the frame named followed by the method name sets properties for a frame. The following program code shows how to set the properties of a frame window:

Setting the Properties of a Frame
++++++++++++++++++++++++++++++++++++++
import java.awt.*;
import java.awt.event.*;
public class Framed extends Frame
{
public static void main(String args[])
{
/* Creating a frame object */
                Framed frmobj=new Framed();
                Framec fc=new Framec(frmobj);
                /*Set frame properties*/
                frmobj.setSize(400,450);
                frmobj.setVisible(true);
                frmobj.setTitle("Demo");
                
                /*Setting the Background color for frame using color object.*/
                Color col = new Color(178,185,255);
                frmobj.setBackground(col);                
                
                /*Add windowListener method to provide window functionality*/
                frmobj.addWindowListener(fc);
}
}
/*Subclass of the windowadapter class is created to
provide window events such as closing a frame or minimizing a frame.*/

class Framec extends WindowAdapter
{
/*Object of the frame subclass is created*/

Framed fd;

/*Object of frame subclass is passed as arguments in the windowadpter method*/

Framec(Framed f)
{
fd = f;
}
/* Closing event is defined for frame.*/
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
++++++++++++++++++++++++++++++++++++++
Saved file with Framed.java and follow below commands:

C:\>jdk1.4\bin>javac Framed.java
C:\>jdk1.4\bin>java Framed



Canvas
This is als a Special type of Class which is also derived from a Windows Class and this is not a part of AWT hierarchy class and this is one another type of window which is used for creating a blank Area and this will Creates an Empty Area for doing drawing. It provides us a blank window which can be drawn upon it. And it inherits the Properties from the Paint Method from the Graphics Class which is Located in the Component Class.


No comments:

Post a Comment