session3 - awt

Upload: megha-bhatia

Post on 07-Apr-2018

244 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 Session3 - AWT

    1/29

    Abstract Windowing Toolkit

    Session 3

  • 8/4/2019 Session3 - AWT

    2/29

    Abstract Windowing Toolkit / 2 of 29

    Objectives

    Discuss Containers and Components

    Frame Panel

    Label

    TextFields and TextAreas

    Checkboxes and RadioButtons

    Choice

    Identify events generated by components

    Create a standalone AWT application

    Describe Abstract Windowing Toolkit (AWT)

  • 8/4/2019 Session3 - AWT

    3/29

    Abstract Windowing Toolkit / 3 of 29

    Introduction

    These make the language richer in terms of creatinggraphical objects that can be controlled by thedeveloper as well as the user.

    One of the most important outcomes of this is thatlanguages have happened to be GUI based.

    In this session we discuss how Java supports GUIfeatures and how they are implemented.

    As on date, software applications have become user-friendlier because of introducing graphics in user

    interfaces

  • 8/4/2019 Session3 - AWT

    4/29

    Abstract Windowing Toolkit / 4 of 29

    Abstract Windowing Toolkit

    Abstract Windowing Toolkit (AWT) is a setof Java classes that allow us to create a GUI

    AWT provides items which enable creation ofan attractive and efficient GUI

    Graphical User Interface (GUI) is used to

    accept input through a keyboard or a mouse

  • 8/4/2019 Session3 - AWT

    5/29

    Abstract Windowing Toolkit / 5 of 29

    Containers

    It can be drawn or painted Container class in the java.awt package

    directly or indirectly derives two commonlyused containers Frame and Panel

    Frame is a separate window and has borders

    Panel is an area without borders and iscontained within a window

    An area that can hold elements

  • 8/4/2019 Session3 - AWT

    6/29

    Abstract Windowing Toolkit / 6 of 29

    Containers - Frame

    Can be a component or a container

    Can be created using constructors

    Some of these constructors are: Frame()

    Creates a Frame which is invisible

    Frame(String Title)

    Creates an invisible Frame with given title

    A window that is independent of an applet

    and of the browser

  • 8/4/2019 Session3 - AWT

    7/29Abstract Windowing Toolkit / 7 of 29

    Example

    Output

  • 8/4/2019 Session3 - AWT

    8/29Abstract Windowing Toolkit / 8 of 29

    Containers - Panel

    Simplest way to create a panel is through itsconstructor Panel( )

    A panel has to be added to a frame The frame will be visible only when the two

    methods setSize( ) and setVisible( ) are set

    Used to group a number of components

    together

  • 8/4/2019 Session3 - AWT

    9/29Abstract Windowing Toolkit / 9 of 29

    Example

    Output

  • 8/4/2019 Session3 - AWT

    10/29Abstract Windowing Toolkit / 10 of 29

    Component

    Examples include textfields, labels,checkboxes, textareas etc.

    Some advanced components includescrollbars, scrollpanes and dialogs

    Anything that can be placed on a user

    interface and can be made visible or resized

  • 8/4/2019 Session3 - AWT

    11/29Abstract Windowing Toolkit / 11 of 29

    Hierarchy of classes in JavaComponent

    Button Checkbox Container Choice Canvas

    TextComponent

    Label

    Panel Window

    Applet Frame Dialog

    TextArea TextField

  • 8/4/2019 Session3 - AWT

    12/29Abstract Windowing Toolkit / 12 of 29

    Various components

    Label Text field

    Checkbox

    Radio button

    Button

    Text Area

  • 8/4/2019 Session3 - AWT

    13/29Abstract Windowing Toolkit / 13 of 29

    Label

    Not user-editable

    Can be created using one of the following constructors: Label( )

    Creates an empty label

    Label(String labeltext)

    Creates a label with a given text

    Label(String labeltext, int alignment)Creates a label with given alignment where alignment

    can be Label.LEFT, Label.RIGHT or Label.CENTER

    Generally used to indicate the purpose of an item

  • 8/4/2019 Session3 - AWT

    14/29Abstract Windowing Toolkit / 14 of 29

    Textfield

    Generally accepts one line of input

    Can be created using one of the following constructors: Textfield()

    Creates a new textfield

    Textfield(int columns)Creates a new textfield with given number of columns

    Textfield(String s)Creates a new textfield with the given string

    Textfield(String s, int columns)Creates a new textfield with given string and given

    number of columns

    GUI element used to input text

  • 8/4/2019 Session3 - AWT

    15/29

    Abstract Windowing Toolkit / 15 of 29

    Example

    Output

  • 8/4/2019 Session3 - AWT

    16/29

    Abstract Windowing Toolkit / 16 of 29

    TextArea

    Includes a scrollbar

    TextArea can be created using some of the followingconstructors given below: TextArea( )

    Creates a new TextArea

    TextArea(int rows, int cols)

    Creates a new TextArea with given number of rows andcolumns

    TextArea(String text, int rows, int cols)Creates a new TextArea with given string, given number

    of rows and columns

    Used when text is to be accepted has two or more lines

  • 8/4/2019 Session3 - AWT

    17/29

    Abstract Windowing Toolkit / 17 of 29

    Example

    Output

  • 8/4/2019 Session3 - AWT

    18/29

    Abstract Windowing Toolkit / 18 of 29

    Button

    The easiest way to trap user action Can create buttons in Java using any of the

    following constructors

    Button()

    Creates a new Button.

    Button(String text)

    Creates a new Button with the given String

    Part of GUI

  • 8/4/2019 Session3 - AWT

    19/29

    Abstract Windowing Toolkit / 19 of 29

    Example

    Output

  • 8/4/2019 Session3 - AWT

    20/29

    Abstract Windowing Toolkit / 20 of 29

    Checkbox

    Checkboxes in Java can be created usingconstructors.

    Some of these constructors are: Checkbox()

    Creates an empty textbox

    Checkbox(String text)Creates a checkbox with given string as label

    Used for multi-option user input that the usermay select or deselect by clicking them.

  • 8/4/2019 Session3 - AWT

    21/29

    Abstract Windowing Toolkit / 21 of 29

    Example

    Output

  • 8/4/2019 Session3 - AWT

    22/29

    Abstract Windowing Toolkit / 22 of 29

    Radiobuttons

    Only one button in a radiobutton group can

    be selected. First create a CheckboxGroup object

    CheckboxGroup cg=new CheckboxGroup();

    Then create each of the radio buttons Checkbox male=Checkbox(male,cg,true);

    Checkbox female=Checkbox(female,cg,false);

    Used as option button to specify choices.

  • 8/4/2019 Session3 - AWT

    23/29

    Abstract Windowing Toolkit / 23 of 29

    Example

    Output

  • 8/4/2019 Session3 - AWT

    24/29

    Abstract Windowing Toolkit / 24 of 29

    Lists

    User may select one or more items

    Created using a number of strings or textvalues

    Choice class enables us to create multipleitem lists

    Choice moviestars=new Choice();

    Add items using the addItem() method

    moviestars.addItem(Antonio Banderas);

    moviestars.addItem(Leonardo Dicaprio);

    Displays a list of choices to the user

  • 8/4/2019 Session3 - AWT

    25/29

    Abstract Windowing Toolkit / 25 of 29

    Example

    Output

  • 8/4/2019 Session3 - AWT

    26/29

    Abstract Windowing Toolkit / 26 of 29

    Event handling

    with components (1)

    Event Delegation Model is used to handleevents

    This process allows the program to register

    handlers called listeners with objects whoseevents need to be captured

    Handlers are automatically called when an

    event takes place

    An event is generated whenever a user clicks amouse, presses or releases a key

  • 8/4/2019 Session3 - AWT

    27/29

    Abstract Windowing Toolkit / 27 of 29

    Event handling

    with components (2)

    Steps to be followed

    Associate the class with the appropriate listenerinterface

    Identify all components that generate events Identify all events to be handled

    Implement the methods of the listener and writethe event handling code within the methods

    Event listeners are implemented as interfaces

    in Java

  • 8/4/2019 Session3 - AWT

    28/29

    Abstract Windowing Toolkit / 28 of 29

    Event handling

    with components (3)Event Class Description Interface

    ActionEvent Button is pressed, list item isdouble clicked or a menu isselected

    ActionListener

    AdjustmentEvent When scrollbar is used AdjustmentListener

    ComponentEvent Component is resized,

    moved, hidden or madevisible

    ComponentListener

    FocusEvent Component gains or loseskeyboard focus

    FocusListener

  • 8/4/2019 Session3 - AWT

    29/29

    Event handling

    with components (4)Event Class Description Interface

    ItemEvent When a menu item is selectedor deselected or when a

    checkbox or list item is clicked

    ItemListener

    WindowEvent Window is activated, closed,opened or quit

    WindowListener

    TextEvent Value of a text field or text areais changed

    TextListener

    MouseEvent Mouse is moved, clicked,dragged or released

    MouseListener MouseMotionListener

    KeyEvent Input is received from thekeyboard

    KeyListener