KEMBAR78
Lecture 2 Introduction to AWT (1).ppt.hello | PPTX
Abstract Windowing Toolkit
CHAPTER 1
RAISONI GROUP OF INSTITUTIONS 1
Abstract Windowing Toolkit
Abstract Window Toolkit
Abstract Windowing Toolkit:
 Java AWT is an API to develop GUI or windows based applications in java.
 The AWT contains numerous classes and methods that allow us to create and manage 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 components are platform-dependent.
 AWT is heavyweight i.e. its components are using the resources of OS.
 The java.awt package provides classes for AWT.
RAISONI GROUP OF INSTITUTIONS 2
Abstract Window Toolkit
RAISONI GROUP OF INSTITUTIONS 3
AWT Controls
 There are various graphical components that can be placed on the frame.
 These components has the classes & classes has corresponding methods.
 We can place AWT Controls on Frame window or applet window using add method.
 When we place the components on the frame we need to set the layout of the frame.
 The commonly used layout is Flow Layout.
 Various components that can be placed for designing user interface are
RAISONI GROUP OF INSTITUTIONS 4
1. Label 2.Buttons 3.Scrollbars 4.Text Components 5.Checkbox
6.Choices 7.List panels 8.Dialogs 9.Menubar
Frame
 Frame is a standard graphical window
 Frame can be displayed using the Frame class
 The frame drawn using this class has standard minimize,maximize and close buttons.
The syntax of frame class is:
Frame() : This creates the new instance of frame which is invisible
initially.
Frame(String Title): This creates the new instance of frame which has some title.
RAISONI GROUP OF INSTITUTIONS 5
Frame Continued…..
Methods of Frame Class
RAISONI GROUP OF INSTITUTIONS 6
Methods Description
void setResizable(Boolean resizable) Sets frame to resizable
Void setTitle(String Title) Sets the title of the frame
Void setSize(int width,int height) Sets the width and height of a frame
String getTitle() Obtains the title of the frame
Void setVisible(Boolean visible) Set the frame visible or not
Frame Continued…..
There are two ways to create the Frames in AWT
Creating Frame
Extending Frame Class Using instance of Frame Class
RAISONI GROUP OF INSTITUTIONS 7
Frame
 Program to create java frame by using an instance of Frame class
import java.awt.*;
class Myframe1
{
public static void main(String args[])
{
Frame f=new Frame();
f.setVisible(true);
f.setSize(400,200);
}
}
Output
RAISONI GROUP OF INSTITUTIONS 8
Labels
 It is used to represent a single line read only text.
 User can not change this text.
Syntax:
Label(String s)
Label(String s, int style)
 Where s is string contained by the label
 Style is constant used for the style of label.
 It can be Label. RIGHT, Label.LEFT,Label.CENTER
RAISONI GROUP OF INSTITUTIONS 9
Label
 Program to demonstrate the use of Label Component
import java.awt.*;
class Label_demo
{
public static void main(String args[])
{
Frame f=new Frame("Label Demo Program");
f.setSize(300,200);
f.setVisible(true); f.setLayout(new
FlowLayout()); Label l1=new
Label("OK"); Label l2=new
Label("Cancel"); f.add(l1);
f.add(l2);
}
}
Output
RAISONI GROUP OF INSTITUTIONS 10
RAISONI GROUP OF INSTITUTIONS 11
Buttons
 Buttons are the push buttons.
 This component contains a label and when it is pressed it generates an event.
Syntax: Button(String s)
Program to demonstrate the use of Button Component
import java.awt.*;
class Button_demo
{
public static void main(String args[])
{
Frame f=new Frame(“Button Demo Program”);
f.setVisible(true);
f.setSize(400,200);
f.setLayout(new FlowLayout());
Button b1=new Button("YES");
Button b2=new Button("NO");
f.add(b1);
f.add(b2); }}
Output
CheckBox
 This component can be ticked or not ticked
 We can select particular item using checkbox control.
 This control appears as small box along with label.
 The Label tells us the name of the item to be selected.
Syntax:
Checkbox(String label)
where label denotes the label associated with each checkbox.
To get the state of the checkbox the getState() method can be used.
RAISONI GROUP OF INSTITUTIONS 12
Checkbox Group
 Checkbox group component allows the user to make one and only one selection at a time.
 It also called as radio buttons.
Syntax:
Checkbox(String s,CheckboxGroup cbg,Boolean val);
RAISONI GROUP OF INSTITUTIONS 13
Program to demonstrate the use of Checkbox Component.
Program to demonstrate the use of CheckboxGroup component
Scrollbars
 There are two styles of scrollbars- Horizontal scrollbar and Vertical scrollbar.
 Scrollbars allows users to scroll the components.
 Scroll bars are used to select continuous values between a specified minimum and maximum.
 Scroll bars may be oriented horizontally or vertically.
Syntax:
Scrollbar() : Creates a new scrollbar
Scrollbar(int style) : Creates a scroll bar with specified orientation that is Scrollbar.HORIZONTAL or
Scrollbar.VERTICAL
Program to Demonstrate use of scrollbar
RAISONI GROUP OF INSTITUTIONS 14
TextField
 TextField is a slot in which one line text can be entered
 In the Textfield you can enter string, modify it, copy it or paste it.
 Textfield is a subclass of TextComponent
Syntax:
TextField (int n)
where n is total number of characters in the string.
Program to demonstrate Textfield
RAISONI GROUP OF INSTITUTIONS 15
TextArea
 TextArea control is used to handle multi-line text.
Syntax:
TextArea(int n, int m)
where n is for number of lines and m is for number of characters.
Program to demonstrate use of TextArea
RAISONI GROUP OF INSTITUTIONS 16
Lists
 List class provides a compact, multiple choice, scrolling selection list.
 List object can be constructed to show any number of choices in the visible window.
 It can also be created to allow multiple selections.
Syntax:
List(): creates a new scrolling list
List(int rows): creates a new scrolling list initialized with specified number of visible lines.
List(int rows, Boolean multimode): creates a new scrolling list initialized with specified number of
visible lines.
Program to Demonstrate List Control
RAISONI GROUP OF INSTITUTIONS 17
Choice Control
 Choice class is used to create a pop-up list of items from which the user may choose.
 It is a form of Menu.
 When user clicks on it ,the whole list of choices pops up and a new selection can be made.
Syntax:
Choice(): creates an empty choice box.
Program to demonstrate Choice control
RAISONI GROUP OF INSTITUTIONS 18
Adding Control On Applet Window
 Frame has Title Bar and three icons on the title bar –Minimize, Maximize and Close.
 Close option does not work ,explicitly we use ctrl+c to close the frame.
 To solve this problem ,one solution is to create a Frame Window within an applet window.
 Applets are the small Java Programs that can be used in internetworking environment.
 An applet depends on the viewer or browser to provide a context in which to run.
Creation of Applet:
 It makes use of two classes – Applet and Graphics
 For the Applet class java.applet package is required.
 For the Graphics class java.awt package is required.
RAISONI GROUP OF INSTITUTIONS 19
Applet
 An applet is a Java program that runs in a Web browser
 An applet is a Java class that extends the java.applet.Applet class.
 A main() method is not invoked on an applet, and an applet class will not define main().
 Applets are designed to be embedded within an HTML page
 A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate
runtime environment.
 The JVM on the user's machine creates an instance of the applet class and invokes various methods
during the applet's lifetime.
RAISONI GROUP OF INSTITUTIONS 20
Life Cycle of an Applet
Four methods in the Applet class gives you the framework on which you build any serious applet −
 init − This method is intended for whatever initialization is needed for your applet. It is called after
the param tags inside the applet tag have been processed.
 start − This method is automatically called after the browser calls the init method. It is also called
whenever the user returns to the page containing the applet after having gone off to other pages.
 stop − This method is automatically called when the user moves off the page on which the applet
sits. It can, therefore, be called repeatedly in the same applet.
 destroy − This method is only called when the browser shuts down normally. Because applets are
meant to live on an HTML page, you should not normally leave resources behind after a user leaves the
page that contains the applet.
 paint − Invoked immediately after the start() method, and also any time the applet needs to repaint
itself in the browser. The paint() method is actually inherited from the java.awt.
RAISONI GROUP OF INSTITUTIONS 21
First Applet Program
/*This is my first applet program*/
import java.awt.*;
import java.applet.*;
public class
FirstApplet extends
Applet
{
public void
paint(Graphics g)
{
g.drawString(“This is
my First
Applet”,50,30);
} RAISONI GROUP OF INSTITUTIONS 22
Execution Of Applet
We can execute the applet using the applet viewer tool
Following are the steps to execute an applet
Step 1: To run applet using command prompt we need to modify the code.
/*This is my first applet program*/
import java.awt.*;
import java.applet.*;
/*<applet code=“FirstApplet” width=300 height=100>
</applet> */
public class FirstApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString(“This is my First Applet”,50,30);
}
}
RAISONI GROUP OF INSTITUTIONS 23
RAISONI GROUP OF INSTITUTIONS 24
RAISONI GROUP OF INSTITUTIONS 25
RAISONI GROUP OF INSTITUTIONS 26
RAISONI GROUP OF INSTITUTIONS 27
RAISONI GROUP OF INSTITUTIONS 28
RAISONI GROUP OF INSTITUTIONS 29
RAISONI GROUP OF INSTITUTIONS 30

Lecture 2 Introduction to AWT (1).ppt.hello

  • 1.
    Abstract Windowing Toolkit CHAPTER1 RAISONI GROUP OF INSTITUTIONS 1 Abstract Windowing Toolkit
  • 2.
    Abstract Window Toolkit AbstractWindowing Toolkit:  Java AWT is an API to develop GUI or windows based applications in java.  The AWT contains numerous classes and methods that allow us to create and manage 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 components are platform-dependent.  AWT is heavyweight i.e. its components are using the resources of OS.  The java.awt package provides classes for AWT. RAISONI GROUP OF INSTITUTIONS 2
  • 3.
    Abstract Window Toolkit RAISONIGROUP OF INSTITUTIONS 3
  • 4.
    AWT Controls  Thereare various graphical components that can be placed on the frame.  These components has the classes & classes has corresponding methods.  We can place AWT Controls on Frame window or applet window using add method.  When we place the components on the frame we need to set the layout of the frame.  The commonly used layout is Flow Layout.  Various components that can be placed for designing user interface are RAISONI GROUP OF INSTITUTIONS 4 1. Label 2.Buttons 3.Scrollbars 4.Text Components 5.Checkbox 6.Choices 7.List panels 8.Dialogs 9.Menubar
  • 5.
    Frame  Frame isa standard graphical window  Frame can be displayed using the Frame class  The frame drawn using this class has standard minimize,maximize and close buttons. The syntax of frame class is: Frame() : This creates the new instance of frame which is invisible initially. Frame(String Title): This creates the new instance of frame which has some title. RAISONI GROUP OF INSTITUTIONS 5
  • 6.
    Frame Continued….. Methods ofFrame Class RAISONI GROUP OF INSTITUTIONS 6 Methods Description void setResizable(Boolean resizable) Sets frame to resizable Void setTitle(String Title) Sets the title of the frame Void setSize(int width,int height) Sets the width and height of a frame String getTitle() Obtains the title of the frame Void setVisible(Boolean visible) Set the frame visible or not
  • 7.
    Frame Continued….. There aretwo ways to create the Frames in AWT Creating Frame Extending Frame Class Using instance of Frame Class RAISONI GROUP OF INSTITUTIONS 7
  • 8.
    Frame  Program tocreate java frame by using an instance of Frame class import java.awt.*; class Myframe1 { public static void main(String args[]) { Frame f=new Frame(); f.setVisible(true); f.setSize(400,200); } } Output RAISONI GROUP OF INSTITUTIONS 8
  • 9.
    Labels  It isused to represent a single line read only text.  User can not change this text. Syntax: Label(String s) Label(String s, int style)  Where s is string contained by the label  Style is constant used for the style of label.  It can be Label. RIGHT, Label.LEFT,Label.CENTER RAISONI GROUP OF INSTITUTIONS 9
  • 10.
    Label  Program todemonstrate the use of Label Component import java.awt.*; class Label_demo { public static void main(String args[]) { Frame f=new Frame("Label Demo Program"); f.setSize(300,200); f.setVisible(true); f.setLayout(new FlowLayout()); Label l1=new Label("OK"); Label l2=new Label("Cancel"); f.add(l1); f.add(l2); } } Output RAISONI GROUP OF INSTITUTIONS 10
  • 11.
    RAISONI GROUP OFINSTITUTIONS 11 Buttons  Buttons are the push buttons.  This component contains a label and when it is pressed it generates an event. Syntax: Button(String s) Program to demonstrate the use of Button Component import java.awt.*; class Button_demo { public static void main(String args[]) { Frame f=new Frame(“Button Demo Program”); f.setVisible(true); f.setSize(400,200); f.setLayout(new FlowLayout()); Button b1=new Button("YES"); Button b2=new Button("NO"); f.add(b1); f.add(b2); }} Output
  • 12.
    CheckBox  This componentcan be ticked or not ticked  We can select particular item using checkbox control.  This control appears as small box along with label.  The Label tells us the name of the item to be selected. Syntax: Checkbox(String label) where label denotes the label associated with each checkbox. To get the state of the checkbox the getState() method can be used. RAISONI GROUP OF INSTITUTIONS 12
  • 13.
    Checkbox Group  Checkboxgroup component allows the user to make one and only one selection at a time.  It also called as radio buttons. Syntax: Checkbox(String s,CheckboxGroup cbg,Boolean val); RAISONI GROUP OF INSTITUTIONS 13 Program to demonstrate the use of Checkbox Component. Program to demonstrate the use of CheckboxGroup component
  • 14.
    Scrollbars  There aretwo styles of scrollbars- Horizontal scrollbar and Vertical scrollbar.  Scrollbars allows users to scroll the components.  Scroll bars are used to select continuous values between a specified minimum and maximum.  Scroll bars may be oriented horizontally or vertically. Syntax: Scrollbar() : Creates a new scrollbar Scrollbar(int style) : Creates a scroll bar with specified orientation that is Scrollbar.HORIZONTAL or Scrollbar.VERTICAL Program to Demonstrate use of scrollbar RAISONI GROUP OF INSTITUTIONS 14
  • 15.
    TextField  TextField isa slot in which one line text can be entered  In the Textfield you can enter string, modify it, copy it or paste it.  Textfield is a subclass of TextComponent Syntax: TextField (int n) where n is total number of characters in the string. Program to demonstrate Textfield RAISONI GROUP OF INSTITUTIONS 15
  • 16.
    TextArea  TextArea controlis used to handle multi-line text. Syntax: TextArea(int n, int m) where n is for number of lines and m is for number of characters. Program to demonstrate use of TextArea RAISONI GROUP OF INSTITUTIONS 16
  • 17.
    Lists  List classprovides a compact, multiple choice, scrolling selection list.  List object can be constructed to show any number of choices in the visible window.  It can also be created to allow multiple selections. Syntax: List(): creates a new scrolling list List(int rows): creates a new scrolling list initialized with specified number of visible lines. List(int rows, Boolean multimode): creates a new scrolling list initialized with specified number of visible lines. Program to Demonstrate List Control RAISONI GROUP OF INSTITUTIONS 17
  • 18.
    Choice Control  Choiceclass is used to create a pop-up list of items from which the user may choose.  It is a form of Menu.  When user clicks on it ,the whole list of choices pops up and a new selection can be made. Syntax: Choice(): creates an empty choice box. Program to demonstrate Choice control RAISONI GROUP OF INSTITUTIONS 18
  • 19.
    Adding Control OnApplet Window  Frame has Title Bar and three icons on the title bar –Minimize, Maximize and Close.  Close option does not work ,explicitly we use ctrl+c to close the frame.  To solve this problem ,one solution is to create a Frame Window within an applet window.  Applets are the small Java Programs that can be used in internetworking environment.  An applet depends on the viewer or browser to provide a context in which to run. Creation of Applet:  It makes use of two classes – Applet and Graphics  For the Applet class java.applet package is required.  For the Graphics class java.awt package is required. RAISONI GROUP OF INSTITUTIONS 19
  • 20.
    Applet  An appletis a Java program that runs in a Web browser  An applet is a Java class that extends the java.applet.Applet class.  A main() method is not invoked on an applet, and an applet class will not define main().  Applets are designed to be embedded within an HTML page  A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment.  The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime. RAISONI GROUP OF INSTITUTIONS 20
  • 21.
    Life Cycle ofan Applet Four methods in the Applet class gives you the framework on which you build any serious applet −  init − This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed.  start − This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages.  stop − This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet.  destroy − This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.  paint − Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt. RAISONI GROUP OF INSTITUTIONS 21
  • 22.
    First Applet Program /*Thisis my first applet program*/ import java.awt.*; import java.applet.*; public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString(“This is my First Applet”,50,30); } RAISONI GROUP OF INSTITUTIONS 22
  • 23.
    Execution Of Applet Wecan execute the applet using the applet viewer tool Following are the steps to execute an applet Step 1: To run applet using command prompt we need to modify the code. /*This is my first applet program*/ import java.awt.*; import java.applet.*; /*<applet code=“FirstApplet” width=300 height=100> </applet> */ public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString(“This is my First Applet”,50,30); } } RAISONI GROUP OF INSTITUTIONS 23
  • 24.
    RAISONI GROUP OFINSTITUTIONS 24
  • 25.
    RAISONI GROUP OFINSTITUTIONS 25
  • 26.
    RAISONI GROUP OFINSTITUTIONS 26
  • 27.
    RAISONI GROUP OFINSTITUTIONS 27
  • 28.
    RAISONI GROUP OFINSTITUTIONS 28
  • 29.
    RAISONI GROUP OFINSTITUTIONS 29
  • 30.
    RAISONI GROUP OFINSTITUTIONS 30