Search This Blog

Get started with AWT

DownLoad : Click here

AWT (Abstract Window Toolkit)

Concepts :
  • CUI vs GUI
  •  Packages
  • Get Started with Awt  or steps
  • Types of Classes
  • Event Delegation Model
  • Example of an GUI

1. CUI vs GUI :

CUI stands for Character User Interface , It is used for developing an Command Applications e.g. M.S.DOS.
GUI stands for Graphical User Interface , It is used for developing WYSIWYG(What you see what you get) or look and feel Applications e.g. M.S.WINDOWS

2.Packages for developing GUI :

  • Java.applet.*;
  • Java.awt.*;
  • Javax.swing.*;
3. Get started with AWT or steps :

Step-1 :
                Create an instance of a component class which is required to you.

Eg : Label lb=new Label();
       TextField tf=new TextField();
        Button b1=new Button(); etc
Step-2:
                Add the component class  to Frame.
Eg :  add(lb);
         add(tf);
         add(b1);

Step-3:
                Add the related Listener to related Component
Eg:  b1.addActionListener();

4. Types of Classes :
  • Component class
  • Container class
  • Helper Class
Component Class:                                                                                           
Java.awt.Label  // Label                                                                                   
Java.awt.Button  //  Button                                                                              
Java.awt.TextField  // TextField
Java.awt.TextArea // TextArea                                                                         
Java.awt.Choice // Choice                                                                                
Java.awt.List // List                                                                                          
Java.awt.CheckBox // CheckBox
Java.awt.CheckBoxGroup // CheckBoxGroup
Java.awt.ScrollBar // ScrollBar

Container Class
java.awt.Frame
java.awt.Panel

Helper Class
java.awt.Font
java.awt.Color


Component class
Listener / Interface
Listener Method
Label

Button


CheckBox


CheckBoxGroup


TextField






TextArea





Choice




List





ScrollBar








Frame














Keyboard Listener
No Listener

ActionListener


Item Listener


Item Listener


FocusListener


ActionListener



FocusListener


ActionListener


ItemListener

ActionListener


ItemListener


 ActionListener


Adjustment Listener



MouseMotionListener




windowListener














KeyListener
-

Public void actionPerformed(ActionEvent ae)


Public void ItemStateChanged(ItemEvent ie)


Public void ItemStateChanged(ItemEvent ie)
 
  
Public void FocusGained(FocusEvent fe)
Public void FocusLost(FocusEvent fe)

Public void actionPerformed(ActionEvent ae)



Public void FocusGained(FocusEvent fe)
Public void FocusLost(FocusEvent fe)

Public void actionPerformed(ActionEvent ae)


Public void ItemStateChanged(ItemEvent ie)

Public void actionPerformed(ActionEvent ae)


Public void ItemStateChanged(ItemEvent ie)


Public void actionPerformed(ActionEvent ae)


Public void adjustmentValueChanged(AdjustmentEvent ae)

Public void MouseMoved(MouseEvent me)




Public void windowActivated(WindowEvent we)

Public void windowDeactvivated(WindowEvent we)

Public void windowClosing(WindowEvent we)

Public void windowClosed(WindowEvent we)

Public void windowOpened(WindowEvent we)


Public void KeyPressed(KeyEvent ke)
Public void KeyReleased(KeyEvent ke)
Public void KeyTyped(KeyEvent ke)


Source code for Closing the window:

Import java.awt.*;
Import java.awt.event.*;
Public class FirstFrame extends Frame implements ActionListener
{
FirstFrame()
{
setVisible(true);
setSize(300,100);
setLocation(100,300);
addWindowListener(new  windowAdapter()
{
Public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
Public static void main(String args[])
{
FirstFrame f=new FirstFrame();
}
}

Source Of Each Component :

Label :

                             Label is a class which is present in java.awt package. It is used to display the text onto the frame.

 Syntax :
                   Label obj=new Label("Text");
                   Label obj=new Label("Text",Label.CENTER); // for print the text at center of the frame
                   Label obj=new Label("Text",Label.LEFT);
                   Label obj=new Label("Text",Label.RIGHT);

Download Source


Example for Source Code:
               
import java.awt.*;
import java.awt.event.*;
public class LabelDemo extends Frame
{
public static void main(String args[])
{
Label lb1=new Label("Green");
Label lb2=new Label("Blue");
Label lb3=new Label("Red");

Panel p1=new Panel(new FlowLayout());

p1.add(lb1);
p1.add(lb2);
p1.add(lb3);

Frame f=new Frame();

f.add(p1);
f.setVisible(true);
f.setSize(300,300);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
}

Compiling and Running the above Program :

File Name : LabelDemo.java
Compiling from command prompt : javac LabelDemo.java
Running from command prompt : java LabelDemo

OUTPUT :



Button :

import java.awt.*;
import java.awt.event.*;
public class ButtonDemo extends Frame
{
public static void main(String args[])
{
Button b1=new Button("Submit");
Panel p1=new Panel(new FlowLayout());

p1.add(b1);

Frame f=new Frame();

f.add(p1);
f.setVisible(true);
f.setSize(300,300);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
}

Compiling and Running the above Program :

File Name : ButtonDemo.java
Compiling from command prompt : javac ButtonDemo.java
Running from command prompt : java ButtonDemo

Output :


TextField Examples with Labels and Button :


import java.awt.*;import java.awt.event.*;
public class Task1 extends Frame
{
Label l1,l2,l3;
Button b1,b2;
TextField tf1,tf2;
Panel p1,p2,p3;
JOptionPane p;
Task1()
{
l1=new Label("Login Page",Label.CENTER);
l2=new Label("UserName :");
l3=new Label("PassWord :");

b1=new Button("Log-in");
b2=new Button("Cancel");

tf1=new TextField(40);
tf2=new TextField(40);
tf2.setEchoChar('*');

p1=new Panel();
p1.setLayout(new FlowLayout());
p2=new Panel();
p2.setLayout(new GridLayout(3,2));
p3=new Panel();
p3.setLayout(new FlowLayout());
p1.add(l1);
p2.add(l2);
p2.add(tf1);
p2.add(l3);
p2.add(tf2);
p3.add(b1);
p3.add(b2);

add(p1);
add(p2);
add(p3);

setLayout(new GridLayout(3,1));
setVisible(true);
setSize(300,190);
setTitle("Login");
setLocation(300,150);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public static void main(String args[])
{
Task1 t=new Task1();
}
}

Compiling and Running the above Program :

File Name : Task1.java
Compiling from command prompt : javac Task1.java
Running from command prompt : javaTask1

Output :



Download here : Love-Calculator Application



Download here : Love-Calculator Application

No comments:

Post a Comment