In JAVA we can develop to types of GUI (Graphic User Interface) applications. They are standalone GUI applications and distributed GUI applications.
As a part of GUI applications we use to create two types of components. They are passive components and active components.
The active and passive components in JAVA are available in terms of classes. In order to deal with any GUI applications we must import a package called java.awt.* (contains various classes and interfaces for creating GUI components) and java.awt.event.* (contains various classes and interfaces which will provide functionality to GUI components).
Whenever we develop any GUI application we must have readily available window component and window component must contain frame component. Any GUI component which we want to create gives the corresponding class and adds all those components to the object of Container class.
A Container is a class whose object allows us to add 'n' number of similar or different GUI components to make a final application.
Object class and Applet class all the classes in the above hierarchy chart are belongs to java.awt.* package.
Write a JAVA program which creates Window and Frame?
Answer:
import java.awt.*; class myf extends Frame { Myf () { setText (?AshaKrishna?); ---1 setSize (100, 100); ---2 setBackground (Color, cyan); ---3 setForeground (Color, red); ---4 setVisible (true); ---5 } }; class FDemo { Public static void main (String [] args) { Myf mo=new myf (); } };
The methods in line numbers 1 and 2 defined in java.awt.Window class. The methods in line numbers 3, 4 and 5 are defined in java.awt.Component class.
Label is a class which is used for creating label as a part of windows application. The component label comes under passive component. Labels always improve the functionality and readability of active components. Creating a label is nothing but creating an object of label components.
public static final int LEFT (0) //by default the alignment is always left only public static final int CENTER (1) public static final int RIGHT (2)
The above three statements are called alignment parameters or modifiers.
Label () Label (String) Label (String label name, int alignment modifier)
public void setText (String); public String getText (); public void setAlignment (int); public int getAlignment ();
For example we can write a program as follows:
Label l=new Label (); l=setText ("ENTER EMPNO"); l=setAlignment (Label.CENTER)
or
Label l1=new Label ("ENTER EMPNAME"); String Label=l1.getText (); l1.setAlignment (Label.RIGHT);
or
Label l=new Label ("ENTER EMPSAL", Label.CENTER);
Whenever we want to develop any windows applications one must deal with event delegation model. Event delegation model contains four properties. They are:
In order to process any active components, we must know either name or caption or label or reference of the component (object).
Whenever we interact any active component, the corresponding active component will have one predefined Event class, whose object will be created and that object contains two details:
The general form of every Event class is xxx event.
For example:
Component name | Event name |
---|---|
java.awt.event.ActionEvent | |
choice | java.awt.event.ItemEvent |
textField | java.awt.event.TextEvent |
textArea | java.awt.event.TextEvent |
scrollbar | java.awt.event.AdjustmentEvent |
In order to perform any action or operation when an interactive component is interacted we must write some set of statements into the appropriate methods. These methods are not defined or developed by SUN micro system, but they have supplied those methods with no definition i.e., undefined methods.
In JAVA purely undefined methods are abstract methods present in interfaces. Interfaces in awt are called listeners. Hence, every interactive component must have a predefined listener whose general notation is xxx listener.
For example:
Component name | Listener name |
---|---|
Button | java.awt.event.ActionListener |
choice | java.awt.event.ItemListener |
textField | java.awt.event.TextListener |
textArea | java.awt.event.TextListener |
scrollbar | java.awt.event.AdjustmentListener |
Identity what is the undefined method or abstract method in xxx Listener.
For example:
Component name | Undefined method name |
---|---|
Button | public void actionPerformed (java.awt.event.ActionEvent) |
choice | public void actionPerformed (java.awt.event.ItemEvent) |
textField | public void actionPerformed (java.awt.event.TextEvent) |
textArea | public void actionPerformed (java.awt.event.TextEvent) |
Each and every interactive component must be registered and unregistered with particular event and Listener. The general form of registration and un-registration methods is as follows:
public void addxxxListener (xxxListener); public void removexxxListener (xxxListener);
For example:
Component name | Registration method | Un-registration method |
---|---|---|
Button | public void addActionListener (ActionListener); | public void removeActionListener (ActionListener); |
choice | public void addItemListener (ItemListener); | public void removeItemListener (ItemListener); |
textField | public void addTextListener (TextListener); | public void removeTextListener (TextListener); |
textArea | public void addTextListener (TextListener); | public void removeTextListener (TextListener); |