1 / 46

Chapter 6: Graphical User Interface GUI and Object-Oriented Design OOD

Java Programming: From Problem Analysis to Program Design, Second Edition. 2. Chapter Objectives. Learn about basic GUI components.Explore how the GUI components JFrame, JLabel, JTextField, and JButton work.Become familiar with the concept of event-driven programming.. Java Programming: From Probl

mead
Télécharger la présentation

Chapter 6: Graphical User Interface GUI and Object-Oriented Design OOD

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) Java Programming: From Problem Analysis to Program Design, Second Edition

    2. Java Programming: From Problem Analysis to Program Design, Second Edition 2 Chapter Objectives Learn about basic GUI components. Explore how the GUI components JFrame, JLabel, JTextField, and JButton work. Become familiar with the concept of event-driven programming.

    3. Java Programming: From Problem Analysis to Program Design, Second Edition 3 Chapter Objectives Discover events and event handlers. Explore object-oriented design. Learn how to identify objects, classes, and members of a class.

    4. Java Programming: From Problem Analysis to Program Design, Second Edition 4 Graphical User Interface (GUI) Components View inputs and outputs simultaneously. One graphical window. Input values in any order. Change input values in window. Click buttons to get output.

    5. Java Programming: From Problem Analysis to Program Design, Second Edition 5 Java GUI Components

    6. Java Programming: From Problem Analysis to Program Design, Second Edition 6 Graphical User Interface (GUI) Components GUI components placed in content pane. GUI components: Windows Labels Text areas Buttons

    7. Java Programming: From Problem Analysis to Program Design, Second Edition 7 GUI Components Added to content pane of window. Not added to window itself. Pixel: A picture element.

    8. Java Programming: From Problem Analysis to Program Design, Second Edition 8 Windows Can be created using a Frame object. The class Frame provides various methods to control attributes of a window. Measured in pixels of height and width. Attributes associated with windows: Title Width Height

    9. Java Programming: From Problem Analysis to Program Design, Second Edition 9 class JFrame GUI window instance created as instance of Frame. Provides various methods to control window attributes.

    10. Java Programming: From Problem Analysis to Program Design, Second Edition 10 Methods Provided by the class JFrame

    11. Java Programming: From Problem Analysis to Program Design, Second Edition 11 Methods Provided by the class JFrame

    12. Java Programming: From Problem Analysis to Program Design, Second Edition 12 Two Ways to Create a Window First way: Declare object of type JFrame. Instantiate object. Use various methods to manipulate window. Second way: Create class-containing application program by extending definition of class JFrame. Utilize mechanism of inheritance.

    13. Java Programming: From Problem Analysis to Program Design, Second Edition 13 Content Pane Inner area of GUI window (below title bar, inside border). To access content pane: Declare reference variable of type Container. Use method getContentPane of class JFrame.

    14. Java Programming: From Problem Analysis to Program Design, Second Edition 14 Methods Provided by the class Container

    15. Java Programming: From Problem Analysis to Program Design, Second Edition 15 class JLabel Labels: Objects of a particular class type. class JLabel: Used to create labels. Label attributes: Title Width Height To create a label: Instantiate object of type JLabel. Modify attributes to control display of labels.

    16. Java Programming: From Problem Analysis to Program Design, Second Edition 16 Methods Provided by the class JLabel

    17. Java Programming: From Problem Analysis to Program Design, Second Edition 17 Methods Provided by the class JLabel

    18. Java Programming: From Problem Analysis to Program Design, Second Edition 18 class JTextField Text fields: Objects belonging to class JTextField. To create a text field: Declare reference variable of type JTextField. Instantiate object.

    19. Java Programming: From Problem Analysis to Program Design, Second Edition 19 Methods Provided by the class JTextField

    20. Java Programming: From Problem Analysis to Program Design, Second Edition 20 class JButton Provided to create buttons in Java. To create a button: Use the same technique that is used to create JLabel and JTextField.

    21. Java Programming: From Problem Analysis to Program Design, Second Edition 21 Methods Provided by the class JButton

    22. Java Programming: From Problem Analysis to Program Design, Second Edition 22 Methods Provided by the class JButton

    23. Java Programming: From Problem Analysis to Program Design, Second Edition 23 Handling an Event Action event: An event created when JButton is clicked. Event listener: An object that receives a message when JButton is clicked. In Java, you must register the listener.

    24. Java Programming: From Problem Analysis to Program Design, Second Edition 24 Handling an Event class ActionListener Handles action event. Part of package java.awt.Event. The class ActionListener is a special type of class (interface). Must contain the actionPerformed method.

    25. Java Programming: From Problem Analysis to Program Design, Second Edition 25 Rectangle Program: Sample Run

    26. Java Programming: From Problem Analysis to Program Design, Second Edition 26 Programming Example: Temperature Conversion Input: Temperature in Fahrenheit or Celsius. Output: Temperature in Celsius if input is Fahrenheit; temperature in Fahrenheit if input is Celsius.

    27. Java Programming: From Problem Analysis to Program Design, Second Edition 27 Programming Example: Temperature Conversion Solution: Create the appropriate JLabels, JTextFields, JButtons. Add them to the created content pane. Calculate the appropriate conversions when the buttons are clicked and an event is triggered.

    28. Java Programming: From Problem Analysis to Program Design, Second Edition 28 Sample Run for TempConversion

    29. Java Programming: From Problem Analysis to Program Design, Second Edition 29 Object-Oriented Design Simplified methodology: Write down detailed description of problem. Identify all (relevant) nouns and verbs. From list of nouns, select objects. Identify data components of each object. From list of verbs, select operations.

    30. Java Programming: From Problem Analysis to Program Design, Second Edition 30 Object-Oriented Design: Example 1 Problem statement: Write a program to input the length and width of a rectangle, and calculate and print the perimeter and area of the rectangle. Nouns: Length, width, rectangle, perimeter, area.

    31. Java Programming: From Problem Analysis to Program Design, Second Edition 31 class Rectangle with Data Members and Operations

    32. Java Programming: From Problem Analysis to Program Design, Second Edition 32 Object-Oriented Design: Example 2 A place to buy candy is from a candy machine. A new candy machine is bought for the gym, but it is not working properly. The candy machine has four dispensers to hold and release items sold by the candy machine and a cash register. The machine sells four products —candies, chips, gum, and cookies—each of which is stored in a separate dispenser. You have been asked to write a program for this candy machine so that it can be put into operation.

    33. Java Programming: From Problem Analysis to Program Design, Second Edition 33 Object-Oriented Design: Example 2 The program should do the following: Show the customer the different products sold by the candy machine. Let the customer make the selection. Show the customer the cost of the item selected. Accept money from the customer. Return change. Release the item, that is, make the sale.

    34. Java Programming: From Problem Analysis to Program Design, Second Edition 34 Object-Oriented Design: Example 2

    35. Java Programming: From Problem Analysis to Program Design, Second Edition 35 Object-Oriented Design: Example 2

    36. Java Programming: From Problem Analysis to Program Design, Second Edition 36 Implementing Classes and Operations Algorithms are used to implement operations. Construct and implement your own methods. Classes Integer, Double, Character, Long, Float: Known as wrapper classes. Provided so that values of primitive data types can be treated as objects. Have limitations (cannot change value stored in objects).

    37. Java Programming: From Problem Analysis to Program Design, Second Edition 37 The class Integer

    38. Java Programming: From Problem Analysis to Program Design, Second Edition 38

More Related