Chapter 9 Visual Programming Basics by ygq15756

VIEWS: 2 PAGES: 41

									   Chapter 9: Visual
Programming Basics
Object-Oriented Program
Development Using Java:
       A Class-Centered
               Approach
                                         Objectives
        • Event-Based Programming

        • Creating a Swing-Based Window

        • Adding a Window Closing Event Handler

        • Adding a Button Component

        • Common Programming Errors

Object-Oriented Program Development Using Java: A Class-Centered Approach   2
               Event-Based Programming
        • Event-based programs provide fully functioning
          GUIs
        • An event is initiated by a user action
        • A program must:
              – Correctly assess which specific event has occurred
              – Provide the appropriate code to perform an action based
                on the identified event


Object-Oriented Program Development Using Java: A Class-Centered Approach   3
Object-Oriented Program Development Using Java: A Class-Centered Approach   4
               Event-Based Programming
                      (continued)
        • Actions that trigger events include:
              – Placing the mouse pointer over a button and clicking
                the left mouse button
              – Using the TAB key until the desired button is
                highlighted with a dotted line then pushing the Enter
                key
              – Pressing an accelerator key
        • The sequence of events in a program are
          controlled by the user

Object-Oriented Program Development Using Java: A Class-Centered Approach   5
               Event-Based Programming
                      (continued)
        • Programmer provides:
              – Code to create GUI
              – Code to appropriately process events
        • Java provides a set of objects for coding GUIs:
              – AWT
                    • Older GUI components
              – Swing
                    • Newer GUI components

Object-Oriented Program Development Using Java: A Class-Centered Approach   6
Object-Oriented Program Development Using Java: A Class-Centered Approach   7
                    The Event-Based Model
        • Operating system:
              – Has total control of computer
              – Never relinquishes control to any executing programs
        • Most executing programs spend the majority of
          their time in a sleep type of mode
        • When an event occurs:
              – The operating system passes event information to the
                appropriate application
              – Permits the application to take action

Object-Oriented Program Development Using Java: A Class-Centered Approach   8
                    Containment Hierarchy
        • Hierarchy of component placement
        • Consists of one and only one top-level container
              – Any number of other intermediate containers
              – And/or atomic components
        • JFrame is most commonly used as a top-level
          container
        • Heavyweight components are responsible for
          interfacing with the operating system

Object-Oriented Program Development Using Java: A Class-Centered Approach   9
                    Containment Hierarchy
                         (continued)
        • A content pane is an internal component provided
          by each top-level container
        • All of the visible components displayed by a GUI
          must be placed on a content pane
        • Menu bar:
              – Can also be added to a top-level container
              – Placed outside of a content pane

Object-Oriented Program Development Using Java: A Class-Centered Approach   10
                    Containment Hierarchy
                         (continued)
        • Layout manager:
              – Defines how components are positioned and sized
                within a container’s content pane
              – Default placement can always be changed
                    • By explicitly specifying another layout manager

        • Lightweight components:
              – Intermediate containers and atomic components
              – Do not interface with the operating system

Object-Oriented Program Development Using Java: A Class-Centered Approach   11
Object-Oriented Program Development Using Java: A Class-Centered Approach   12
Object-Oriented Program Development Using Java: A Class-Centered Approach   13
Object-Oriented Program Development Using Java: A Class-Centered Approach   14
                    Creating a Swing-Based
                           Window
        • Two predominant approaches:

              – Construct a GUI as a separate class using Swing
                components

              – Construct a GUI object using Swing components from
                within the main() method




Object-Oriented Program Development Using Java: A Class-Centered Approach   15
                    Creating a Swing-Based
                     Window (continued)
        • Create JFrame:

              – JFrame mainFrame = new JFrame("First GUI
                Window");

        • Setting size:

              – mainFrame.setSize(300,150);



Object-Oriented Program Development Using Java: A Class-Centered Approach   16
Object-Oriented Program Development Using Java: A Class-Centered Approach   17
                    Creating a Swing-Based
                     Window (continued)
        • Display JFrame:

              – Use show()

              – Or setVisible(true)




Object-Oriented Program Development Using Java: A Class-Centered Approach   18
                                   Look and Feel
        • Refers to:
              – How a GUI appears on screen
              – How a user interacts with it

        • Swing package:
              – Supports four look and feel types
              – If no look and feel is specified, the default Java look
                and feel is used


Object-Oriented Program Development Using Java: A Class-Centered Approach   19
Object-Oriented Program Development Using Java: A Class-Centered Approach   20
Object-Oriented Program Development Using Java: A Class-Centered Approach   21
                 Adding a Window Closing
                      Event Handler
        • GUI creation process includes:
              – Phase 1: Construct a component so that it appears
                visually

              – Phase 2: Provide an event handler for the component

        • Event handler
              – Object that responds appropriately when an event
                occurs

Object-Oriented Program Development Using Java: A Class-Centered Approach   22
             The Event Delegation Model
        • Requires two basic elements:
              – Component to generate an event
              – Event handler or listener object
        • The component delegates responsibility to a
          listener object for doing something when an event
          is generated
        • Registration statement
              – Glue that attaches an event to a specific event handler

Object-Oriented Program Development Using Java: A Class-Centered Approach   23
              Phase 2: Provide an Event
              Handler for the Component
        • Step 1: Write the code for an event handler class
              – Known as the listener class

        • Step 2: Create an instance of the event handler class
              – Means instantiating an object of the class using the new
                operator
              – Created object is known as a listener object

        • Step 3: Register the listener object created in step 2
Object-Oriented Program Development Using Java: A Class-Centered Approach   24
                  Coding an Event Handler
        • The event handling class has been coded as a
          separate nonnested class
        • WindowListener interface:
              – Must be implemented by an event handler class
              – Required by Java for handling window events
              – All of the listed methods must be implemented
                    • Even if they consist of empty bodies


Object-Oriented Program Development Using Java: A Class-Centered Approach   25
                  Coding an Event Handler
                        (continued)
        • Add listener to JFrame:

              – mainFrame.addWindowListener(handler);

        • Multiple listener objects can be registered to the
          same event source

        • A single listener object can be registered to
          multiple event sources

Object-Oriented Program Development Using Java: A Class-Centered Approach   26
Object-Oriented Program Development Using Java: A Class-Centered Approach   27
                Adapter and Inner Classes
        • Adapter classes:
              – Declare empty event handling methods for a given
                interface type
              – Can be used as a parent class for a listener class
              – Constructed as abstract classes
        • Inner class:
              – One class is nested inside another class
              – Place the event handler class definition close to other
                GUI-related code

Object-Oriented Program Development Using Java: A Class-Centered Approach   28
                Adapter and Inner Classes
                      (continued)
        • Event-handling code guidelines:

              – Listener class should be made as short as possible

              – Code should be placed as close as possible to where an
                object of the class is actually instantiated




Object-Oriented Program Development Using Java: A Class-Centered Approach   29
                          Anonymous Classes
        • Class without a name
        • Permits placing the event handling class code
          directly into the statement that creates an instance
          of the event handling class
        • Example:
              mainFrame.addWindowListener(new WindowAdapter()
              {public void windowClosing(WindowEvent e)
                {System.exit(0);}});


Object-Oriented Program Development Using Java: A Class-Centered Approach   30
                          Anonymous Classes
                             (continued)
        • Should only be used when an event handler
          consists of a single, short method

        • Always used within a statement that creates an
          instance of the class




Object-Oriented Program Development Using Java: A Class-Centered Approach   31
Object-Oriented Program Development Using Java: A Class-Centered Approach   32
             Adding a Button Component
        • Adding components to GUI:
              – Phase 1: Construct a component so that it appears
                visually
                    • Step 1: Create a specific component
                    • Step 2: Add the component into a container
              – Phase 2: Provide an event handler for the component
                    • Step 1: Write code for an event handler class
                    • Step 2: Create an instance of the event handler class
                    • Step 3: Register the listener object

Object-Oriented Program Development Using Java: A Class-Centered Approach     33
                               Adding a Button
        • When adding lightweight components into a top-
          level container, they must be added to the
          container’s content pane
        • Declare and instantiate JButton:
              – private JButton firstButton;
              – firstButton = new JButton("Press me");
        • Add to the main frame’s content pane:
              – Container c = mainFrame.getContentPane();
              – c.add(firstButton);

Object-Oriented Program Development Using Java: A Class-Centered Approach   34
Object-Oriented Program Development Using Java: A Class-Centered Approach   35
                        Adding ToolTips and
                         Accelerator Keys
        • ToolTip:
              – Single line of text
              – Appears when a user positions the mouse cursor over a
                GUI component
              – Provides a quick single-line documentation for a
                component
              – Syntax:
                    objectName.setToolTipText("string value");


Object-Oriented Program Development Using Java: A Class-Centered Approach   36
               Adding ToolTips and
            Accelerator Keys (continued)
        • Accelerator key:
              – Called a mnemonic key in Java
              – Any key that initiates an action by pressing the Alt key
                and a designated letter
              – Syntax:
                    objectName.setMnemonic('letter');
              – Choose a letter contained in the object’s caption:
                    • Will be underlined in caption
                    • Otherwise hot-key will be “hidden”

Object-Oriented Program Development Using Java: A Class-Centered Approach   37
                 Adding an Event Handler
        • An event handler for an atomic component is
          created and registered in the same way as event
          handlers for a JFrame container

        • ActionListener class is added to JButton

              – Must implement actionPerformed() method

              – Use addActionListener() method to register a handler


Object-Oriented Program Development Using Java: A Class-Centered Approach   38
         Common Programming Errors
        • Forgetting to include GUI-related import
          statements

        • Creating an event handler and failing to register it

        • Modifying a GUI class that has previously been
          compiled and changing its name

              – But forgetting to change the name when an instance of
                the class is created in the main() method

Object-Oriented Program Development Using Java: A Class-Centered Approach   39
                                          Summary
        • Event-based programs execute program code
          depending on what events occur
        • Java handles events triggered by GUI components
          using an event delegation model
        • Graphical components are structured into a Swing-
          based GUI following a containment hierarchy
        • The name of the listener class for each Swing
          component must be a Java-specified name

Object-Oriented Program Development Using Java: A Class-Centered Approach   40
                       Summary (continued)
        • Each implemented listener class requires a specific
          set of methods that must be included
              – Even if this means creating empty method bodies
        • Listener classes can be nested inside the class used
          to instantiate and display GUI components
        • Anonymous classes can be used to construct
          listener classes
              – Whenever a single-statement listener object
                instantiation and registration are employed

Object-Oriented Program Development Using Java: A Class-Centered Approach   41

								
To top