Creating Menus and Button Arrays Using the Abstract Windows Toolkit - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

Creating Menus and Button Arrays Using the Abstract Windows Toolkit

Description:

Keypad buttons share code using a case ... Add constructed Buttons to keypad Panel ... Test the keypad functionality by performing calculations with different ... – PowerPoint PPT presentation

Number of Views:110
Avg rating:3.0/5.0
Slides: 49
Provided by: steve1752
Category:

less

Transcript and Presenter's Notes

Title: Creating Menus and Button Arrays Using the Abstract Windows Toolkit


1
Chapter 6
  • Creating Menus and Button Arrays Using the
    Abstract Windows Toolkit

2
Chapter Objectives
  • Create and implement private variables
  • Include a menu system in a GUI application
  • Manipulate Button arrays
  • Move data in and out of the system clipboard
  • Differentiate between the getActionCommand() and
    get Source() methods

3
Chapter Objectives
  • Write code to search for which component was
    clicked
  • Use multiple layout managers
  • Program multiple case solutions
  • Change the icon in a Java programs title bar
  • Access methods from the Toolkit class

4
Introduction
  • Create a Calculator application
  • Stand-alone
  • Portable
  • Incorporate a Menu system
  • Clear display
  • Exit the application
  • Read about the program
  • Copy and paste data using the system clipboard
  • Implement as a GUI
  • Array of Button objects, TextField, flags

5
Calculator Application
6
(No Transcript)
7
(No Transcript)
8
Problem Analysis
  • Standard window with ability to resize
  • Minimize, Maximum, Close buttons
  • 10 numeric buttons, 4 operator buttons, a decimal
    point, and an equal button
  • LCD screen
  • Displays current number
  • Clears display when an operator button is clicked
  • Displays answer when equal button is clicked
  • Program remembers operators and operands
  • Menu bar with drop-down menu of commands

9
Design the Solution
  • Size and shape of a handheld calculator
  • Title bar with caption and icon
  • TextField for number display
  • BorderLayout layout manager
  • TextField in North region
  • Buttons in Center region
  • Array of buttons placed in Panel using a
    GridLayout
  • Buttons have a label with the number or symbol
  • Menu system with commands

10
(No Transcript)
11
Program Design
  • Button clicks
  • Numeric or decimal point button clicks display in
    the TextField
  • Operator button clicks store the current
    TextField number and the operator
  • Equal sign clicks display the result
  • Declaration and construction of components
  • Majority of logic in ActionListener event
  • Listens and responds to a user click
  • Menu options have individual if statements
  • Keypad buttons share code using a case structure

12
(No Transcript)
13
Private Variables
  • Ensures that a driver class cannot access a
    variable directly
  • Local in scope
  • Used only in the processes of the class in which
    they are declared
  • Protects class from potential user corruption
  • Displays encapsulation
  • Principle of least privilege

14
Overview of coding process
  • Import statements
  • java.awt.datatransfer package moves data in and
    out of the system clipboard
  • Extends Frame and implements ActionListener
  • Constructor method
  • Creates Frame
  • Embeds components
  • Initializes variables
  • Establishes layout manager
  • Sets TextField display
  • Main() method creates an instance of Calculator

15
(No Transcript)
16
Creating Menus
17
(No Transcript)
18
Using Menus
  • File, Edit, and About commands
  • File contains Exit command
  • Edit contains Clear, Copy, and Paste commands
  • About contains About Calculator command
  • Common practice to use the prefix mnu for each
    item with menu bar commands
  • Horizontal separator between Clear and Copy
  • Commands on the menu bar only display their menu
  • This action is polymorphic

19
(No Transcript)
20
(No Transcript)
21
Initializing the Calculator Variables
  • TextField is read-only
  • setEditable() is initialized to false
  • Panel contains keypad Buttons
  • Variables first, op1, clearText and lastOp are
    initialized
  • calcPattern is a DecimalFormat object
  • Initialized to hold a pattern of up to eight
    digits before and after decimal point
  • No commas for easier parsing

22
(No Transcript)
23
Button Arrays and the Keypad
  • Use an array for all Buttons on the keypad
  • Allows the same code for multiple buttons
  • Facilitates searching for buttons
  • A for loop is used to construct and label all the
    numeric buttons
  • Use the valueOf() method to convert the index to
    a String label for the Button
  • Explicitly assign other Buttons
  • Set keypad layout and spacing with GridLayout
    constructor

24
(No Transcript)
25
Adding Components to the Interface
  • Add constructed Buttons to keypad Panel
  • Add buttons row by row from the upper-left
    corner, using a for loop for sequentially ordered
    buttons

26
Adding Components to the Interface
  • Add ActionListener to each Button
  • Use the length property of the array in the for
    loop to make the code more genericThe keyword,
    this, is used to refer to the Button
  • Add Panel and TextField to Frame
  • Frame will add Buttons all at once

27
The addWindowListener() Method
  • Registers the listener with the Frame
  • Allows Frame events to be sent to the listener
  • Creates a new instance of WindowAdapter inside
    the argument of addWindowListener
  • Overrides the windowClosing() method of
    WindowAdapter to terminate the program when the
    Close button is clicked
  • WindowListener is an interface
  • The code creates a WindowAdapter object, which
    implements the interface

28
(No Transcript)
29
The System Clipboard
  • Most operating systems have a clipboard
  • Temporary memory reserved for user storage
  • Can hold more than 20 data pieces of varying
    types
  • Data can be cut, copied, or pasted across
    applications
  • The Clipboard class implements the system
    clipboard to store clipboard contents
  • Methods from the Toolkit class and Transferable
    interface are used to perform the cut, copy, and
    paste operations
  • A Toolkit object binds components to native
    implementations
  • A Transferable object resides in a buffer to
    transfer objects between applications

30
(No Transcript)
31
Selecting Text
  • Copy data to the clipboard by selecting data
  • Normally done by dragging the mouse or using
    SHIFTARROW
  • Can be done in this application by selecting the
    Copy command, since only the TextField can be
    copied
  • The StringSelection class holds the TextField
    data
  • The setContents() method transfers the data to
    the clipboard
  • The first argument is the data
  • The second argument is the current system and
    user

32
The actionPerformed() Method
  • In order to make the menu system and keypad
    functional, an actionPerformed() method must be
    coded
  • ActionListener requires an actionPerformed method
  • When a button or menu option is clicked, the
    ActionListener activates the corresponding
    actionPerformed() method
  • actionPerformed() takes one argument
  • Usually identified by e, which represents the
    object passed to it from ActionListener

33
Searching for Component Clicks
  • setActionCommand() establishes a keyword command
    for each menu item
  • getActionCommand() retrieves this keyword
  • Used to look at individual keywords associated
    with menu items
  • getSource() is associated with ActionEvent
    objects and can be used to compare a components
    variable name
  • Used to look for which of the individual buttons
    was clicked

34
Exit and Clear Commands
35
Copy and Paste Commands
  • The getSystemClipBoard() method is called from
    within a reference to the operating system
    toolkit using the getDefaultToolkit() method
  • The period delimiter is used to link the two
    methods together
  • Assign a variable name to the current clipboard
    each time it is implemented
  • Transfer data to a buffer first and then attempt
    to convert the number to a String
  • Use try blocks to catch non-numeric data
  • The Throwable superclass catches any exception

36
(No Transcript)
37
About Calculator Command
38
Searching the Numeric Buttons
  • Test for button click
  • Sequentially search the array
  • If a match is found, use the case statements to
    determine the action
  • One break statement can serve for multiple cases
  • Set foundKey to true to avoid unnecessary looping
  • To display a number with multiple digits,
    concatenate the result of the getLabel() method
    with the previous data in TextField

39
(No Transcript)
40
Searching for the First Operator Button Click
  • If the first flag is true
  • Save the TextField value in op1
  • Save the operator index in lastOp
  • Set first to false

41
Searching for Subsequent Operator Clicks
  • If the first flag is false
  • A switch structure determines which operator was
    clicked
  • Each case statement performs the corresponding
    calculation
  • The result is stored in op1
  • The DecimalFormat variable formats the result
    before the lcd TextField displays it
  • The clearText flag is set to true to prepare for
    a new calculation

42
(No Transcript)
43
Searching for the Equal Button
  • If the equal button is clicked
  • The first flag is set to true

44
Coding the main() Method
  • Constructs an instance of Calculator
  • Sets attributes with Frame methods

45
Compiling, Running, and Testing the Application
  • Compile the application
  • Test the application using all possible inputs,
    including invalid data
  • Test the keypad functionality by performing
    calculations with different operators and
    multiple operands
  • Test the menu system by clicking on the menu
    commands
  • Test with the system clipboard by copying and
    pasting across applications

46
Chapter Summary
  • Create and implement private variables
  • Include a menu system in a GUI application
  • Manipulate Button arrays
  • Move data in and out of the system clipboard
  • Differentiate between the getActionCommand() and
    get Source() methods

47
Chapter Summary
  • Write code to search for which component was
    clicked
  • Use multiple layout managers
  • Program multiple case solutions
  • Change the icon in a Java programs title bar
  • Access methods from the Toolkit class

48
Chapter 6 Complete
  • Creating Menus and Button Arrays Using the
    Abstract Windows Toolkit
Write a Comment
User Comments (0)
About PowerShow.com