410 likes | 561 Views
Object-Oriented Program Development Using Java: A Class-Centered Approach . 2. Objectives. Event-Based ProgrammingCreating a Swing-Based WindowAdding a Window Closing Event HandlerAdding a Button ComponentCommon Programming Errors. Object-Oriented Program Development Using Java: A Class-Centere
E N D
1. Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach
2. Object-Oriented Program Development Using Java: A Class-Centered Approach 2 Objectives Event-Based Programming
Creating a Swing-Based Window
Adding a Window Closing Event Handler
Adding a Button Component
Common Programming Errors
3. Object-Oriented Program Development Using Java: A Class-Centered Approach 3 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
4. Object-Oriented Program Development Using Java: A Class-Centered Approach 4
5. Object-Oriented Program Development Using Java: A Class-Centered Approach 5 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
6. Object-Oriented Program Development Using Java: A Class-Centered Approach 6 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
7. Object-Oriented Program Development Using Java: A Class-Centered Approach 7
8. Object-Oriented Program Development Using Java: A Class-Centered Approach 8 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
9. Object-Oriented Program Development Using Java: A Class-Centered Approach 9 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
10. Object-Oriented Program Development Using Java: A Class-Centered Approach 10 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
11. Object-Oriented Program Development Using Java: A Class-Centered Approach 11 Containment Hierarchy (continued) Layout manager:
Defines how components are positioned and sized within a containers 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
12. Object-Oriented Program Development Using Java: A Class-Centered Approach 12
13. Object-Oriented Program Development Using Java: A Class-Centered Approach 13
14. Object-Oriented Program Development Using Java: A Class-Centered Approach 14
15. Object-Oriented Program Development Using Java: A Class-Centered Approach 15 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
16. Object-Oriented Program Development Using Java: A Class-Centered Approach 16 Creating a Swing-Based Window (continued) Create JFrame:
JFrame mainFrame = new JFrame("First GUI Window");
Setting size:
mainFrame.setSize(300,150);
17. Object-Oriented Program Development Using Java: A Class-Centered Approach 17
18. Object-Oriented Program Development Using Java: A Class-Centered Approach 18 Creating a Swing-Based Window (continued) Display JFrame:
Use show()
Or setVisible(true)
19. Object-Oriented Program Development Using Java: A Class-Centered Approach 19 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
20. Object-Oriented Program Development Using Java: A Class-Centered Approach 20
21. Object-Oriented Program Development Using Java: A Class-Centered Approach 21
22. Object-Oriented Program Development Using Java: A Class-Centered Approach 22 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
23. Object-Oriented Program Development Using Java: A Class-Centered Approach 23 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
24. Object-Oriented Program Development Using Java: A Class-Centered Approach 24 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
25. Object-Oriented Program Development Using Java: A Class-Centered Approach 25 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
26. Object-Oriented Program Development Using Java: A Class-Centered Approach 26 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
27. Object-Oriented Program Development Using Java: A Class-Centered Approach 27
28. Object-Oriented Program Development Using Java: A Class-Centered Approach 28 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
29. Object-Oriented Program Development Using Java: A Class-Centered Approach 29 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
30. Object-Oriented Program Development Using Java: A Class-Centered Approach 30 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);}});
31. Object-Oriented Program Development Using Java: A Class-Centered Approach 31 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
32. Object-Oriented Program Development Using Java: A Class-Centered Approach 32
33. Object-Oriented Program Development Using Java: A Class-Centered Approach 33 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
34. Object-Oriented Program Development Using Java: A Class-Centered Approach 34 Adding a Button When adding lightweight components into a top-level container, they must be added to the containers content pane
Declare and instantiate JButton:
private JButton firstButton;
firstButton = new JButton("Press me");
Add to the main frames content pane:
Container c = mainFrame.getContentPane();
c.add(firstButton);
35. Object-Oriented Program Development Using Java: A Class-Centered Approach 35
36. Object-Oriented Program Development Using Java: A Class-Centered Approach 36 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");
37. Object-Oriented Program Development Using Java: A Class-Centered Approach 37 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 objects caption:
Will be underlined in caption
Otherwise hot-key will be hidden
38. Object-Oriented Program Development Using Java: A Class-Centered Approach 38 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
39. Object-Oriented Program Development Using Java: A Class-Centered Approach 39 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
40. Object-Oriented Program Development Using Java: A Class-Centered Approach 40 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
41. Object-Oriented Program Development Using Java: A Class-Centered Approach 41 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