Chapter 6 Graphical User Interface GUI and ObjectOriented
											Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) Java Programming: Program Design Including Data Structures
											Chapter Objectives s Learn about basic GUI components s Explore how the GUI components JFrame, JLabel, JText. Field, and JButton work s Become familiar with the concept of event-driven programming Java Programming: Program Design Including Data Structures 2
											Chapter Objectives (continued) s Discover events and event handlers s Explore object-oriented design s Learn how to identify objects, classes, and members of a class Java Programming: Program Design Including Data Structures 3
											Graphical User Interface (GUI) Components s s View inputs and outputs simultaneously One graphical window Input values in any order Change input values in window Click buttons to get output Java Programming: Program Design Including Data Structures 4
											Inheritance Hierarchy Java Programming: Program Design Including Data Structures 5
											Java GUI Components Java Programming: Program Design Including Data Structures 6
											Graphical User Interface (GUI) Components (continued) s GUI components placed in content pane s GUI components: s s Windows Labels Text areas Buttons Java Programming: Program Design Including Data Structures 7
											GUI Components s Added to content pane of window s Not added to window itself s Pixel: A picture element Java Programming: Program Design Including Data Structures 8
											Windows s Can be created using a Frame object s The class Frame provides various methods to control attributes of a window s Measured in pixels of height and width s Attributes associated with windows: s Title s Width s Height Java Programming: Program Design Including Data Structures 9
											class JFrame s GUI window instance created as instance of Frame s Provides various methods to control window attributes Java Programming: Program Design Including Data Structures 10
											Methods Provided by the class JFrame Java Programming: Program Design Including Data Structures 11
											Methods Provided by the class JFrame (continued) Java Programming: Program Design Including Data Structures 12
											Two Ways to Create a Window s First way: s Declare object of type JFrame s Instantiate object s Use various methods to manipulate window s Second way: s Create class-containing application program by extending definition of class JFrame s Utilize mechanism of inheritance Java Programming: Program Design Including Data Structures 13
											Content Pane s Inner area of GUI window (below title bar, inside border) s To access content pane: s Declare reference variable of type Container s Use method get. Content. Pane of class JFrame Java Programming: Program Design Including Data Structures 14
											Methods Provided by the class Container Java Programming: Program Design Including Data Structures 15
											class JLabel s Labels: Objects of a particular class type s class JLabel: Used to create labels s Label attributes: s Title s Width s Height s To create a label: s Instantiate object of type JLabel s Modify attributes to control display of labels Java Programming: Program Design Including Data Structures 16
											Methods Provided by the class JLabel Java Programming: Program Design Including Data Structures 17
											Methods Provided by the class JLabel (continued) Java Programming: Program Design Including Data Structures 18
											class JText. Field s Text fields: Objects belonging to class JText. Field s To create a text field: s Declare reference variable of type JText. Field s Instantiate object Java Programming: Program Design Including Data Structures 19
											Methods Provided by the class JText. Field (continued) Java Programming: Program Design Including Data Structures 20
											class JButton s Provided to create buttons in Java s To create a button: s Use the same technique that is used to create JLabel and JText. Field Java Programming: Program Design Including Data Structures 21
											Methods Provided by the class JButton Java Programming: Program Design Including Data Structures 22
											Methods Provided by the class JButton (continued) Java Programming: Program Design Including Data Structures 23
											Handling an Event s Action event: created when JButton is clicked s Event listener: An object that receives a message when JButton is clicked s In Java, you must register the listener Java Programming: Program Design Including Data Structures 24
											Handling an Event (continued) s class Action. Listener s Handles action event s Part of package java. awt. Event s The class Action. Listener is a special type of class (interface) s Must contain the action. Performed method Java Programming: Program Design Including Data Structures 25
											Rectangle Program: Sample Run Java Programming: Program Design Including Data Structures 26
											Programming Example: Temperature Conversion s Input: Temperature in Fahrenheit or Celsius s Output: Temperature in Celsius if input is Fahrenheit; temperature in Fahrenheit if input is Celsius Java Programming: Program Design Including Data Structures 27
											Programming Example: Temperature Conversion (continued) Solution: 1. Create the appropriate JLabels, JText. Fields, JButtons 2. Add them to the created content pane 3. Calculate the appropriate conversions when the buttons are clicked an event is triggered Java Programming: Program Design Including Data Structures 28
											Sample Run for Temp. Conversion Java Programming: Program Design Including Data Structures 29
											Object-Oriented Design Simplified methodology: 1. 2. 3. 4. 5. 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 Java Programming: Program Design Including Data Structures 30
											Object-Oriented Design: Example 1 s Problem statement: s Write a program to input the length and width of a rectangle, and calculate and print the perimeter and area of the rectangle s Nouns: s Length, width, rectangle, perimeter, area Java Programming: Program Design Including Data Structures 31
											class Rectangle with Data Members and Operations Java Programming: Program Design Including Data Structures 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. Java Programming: Program Design Including Data Structures 33
											Object-Oriented Design: Example 2 The program should do the following: s Show the customer the different products sold by the candy machine s Let the customer make the selection s Show the customer the cost of the item selected s Accept money from the customer s Return change s Release the item, that is, make the sale Java Programming: Program Design Including Data Structures 34
											Object-Oriented Design: Example 2 Java Programming: Program Design Including Data Structures 35
											Object-Oriented Design: Example 2 Java Programming: Program Design Including Data Structures 36
											Implementing Classes and Operations s Algorithms are used to implement operations s Construct and implement your own methods s Classes Integer, Double, Character, Long, Float: s Known as wrapper classes s Provided so that values of primitive data types can be treated as objects s Have limitations (cannot change value stored in objects) Java Programming: Program Design Including Data Structures 37
											The class Integer Java Programming: Program Design Including Data Structures 38
											The class Integer (continued) Java Programming: Program Design Including Data Structures 39
											The class Integer (continued) Integer num; num = new Integer(86) Java Programming: Program Design Including Data Structures 40
											The class Integer (continued) int x; Integer num; num = 25; This statement is equivalent to the statement: num = new Integer(25); The expression: num = 25; is referred to as autoboxing of the int type. Java Programming: Program Design Including Data Structures 41
											The class Integer (continued) int x; Integer num; The statement: x = num; This statement is equivalent to the statement: x = num. int. Value(); This statement is referred to as auto unboxing of the int type. Java Programming: Program Design Including Data Structures 42
											The class Integer (continued) s To compare the values of two Integer objects, you can use the method compare. To s If you want to compare the values of two Integer objects only for equality, then you can use the method equals Java Programming: Program Design Including Data Structures 43
											The class Int. Class Java Programming: Program Design Including Data Structures 44
											The class Int. Class (continued) Java Programming: Program Design Including Data Structures 45
											Chapter Summary s Every GUI contains a window s Various components are added to the content pane of a window s class Frame is used to create windows s JLabel is used to label GUI components and display information to user s JText. Field is used for input/output s JButton generates action event s Action event is sent to action listener s Action listener must have method called action. Performed Java Programming: Program Design Including Data Structures 46
											Chapter Summary (continued) s A class is a collection of data members and methods associated with those members. s Object-oriented design (OOD): s Starts with a problem statement. s Identifies required classes with nouns in problem statement. s Identifies required methods with verbs in problem statement. Java Programming: Program Design Including Data Structures 47
- Slides: 47