Скачать презентацию Chapter 6 Graphical User Interface GUI and Object-Oriented Скачать презентацию Chapter 6 Graphical User Interface GUI and Object-Oriented

d0f90b72fe98a52346471d238c9a4796.ppt

  • Количество слайдов: 46

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

Chapter Objectives s Learn about basic GUI components. s Explore how the GUI components 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 eventdriven programming. Java Programming: From Problem Analysis to Program Design, Second Edition 2

Chapter Objectives s Discover events and event handlers. s Explore object-oriented design. s Learn Chapter Objectives 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: From Problem Analysis to Program Design, Second Edition 3

Graphical User Interface (GUI) Components s s View inputs and outputs simultaneously. One graphical 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: From Problem Analysis to Program Design, Second Edition 4

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

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

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

Windows s Can be created using a Frame object. s The class Frame provides 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: From Problem Analysis to Program Design, Second Edition 8

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

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

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

Two Ways to Create a Window s First way: s Declare object of type 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: From Problem Analysis to Program Design, Second Edition 12

Content Pane s Inner area of GUI window (below title bar, inside border). s 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: From Problem Analysis to Program Design, Second Edition 13

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

class JLabel s Labels: Objects of a particular class type. s class JLabel: Used 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: From Problem Analysis to Program Design, Second Edition 15

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

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

class JText. Field s Text fields: Objects belonging to class JText. Field. s To 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: From Problem Analysis to Program Design, Second Edition 18

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

class JButton s Provided to create buttons in Java. s To create a button: 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: From Problem Analysis to Program Design, Second Edition 20

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

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

Handling an Event s Action event: An event created when JButton is clicked. s Handling an Event s Action event: An 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: From Problem Analysis to Program Design, Second Edition 23

Handling an Event s class Action. Listener s Handles action event. s Part of Handling an Event 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: From Problem Analysis to Program Design, Second Edition 24

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

Programming Example: Temperature Conversion s Input: Temperature in Fahrenheit or Celsius. s Output: Temperature 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: From Problem Analysis to Program Design, Second Edition 26

Programming Example: Temperature Conversion Solution: 1. Create the appropriate JLabels, JText. Fields, JButtons. 2. Programming Example: Temperature Conversion 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: From Problem Analysis to Program Design, Second Edition 27

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

Object-Oriented Design Simplified methodology: 1. 2. 3. 4. 5. Write down detailed description of 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: From Problem Analysis to Program Design, Second Edition 29

Object-Oriented Design: Example 1 s Problem statement: s Write a program to input the 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: From Problem Analysis to Program Design, Second Edition 30

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

Object-Oriented Design: Example 2 A place to buy candy is from a candy machine. 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: From Problem Analysis to Program Design, Second Edition 32

Object-Oriented Design: Example 2 The program should do the following: s Show the customer 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: From Problem Analysis to Program Design, Second Edition 33

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

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

Implementing Classes and Operations s Algorithms are used to implement operations. s Construct and 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: From Problem Analysis to Program Design, Second Edition 36

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

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

The class Integer num; num = new Integer(86) Java Programming: From Problem Analysis to The class Integer num; num = new Integer(86) Java Programming: From Problem Analysis to Program Design, Second Edition 39

The class Integer int x; Integer num; num = 25; This statement is equivalent The class Integer 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: From Problem Analysis to Program Design, Second Edition 40

The class Integer int x; Integer num; The statement: x = num; This statement The class Integer 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: From Problem Analysis to Program Design, Second Edition 41

The class Integer s To compare the values of two Integer objects, you can The class Integer 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 equal. Java Programming: From Problem Analysis to Program Design, Second Edition 42

The class Int. Class Java Programming: From Problem Analysis to Program Design, Second Edition The class Int. Class Java Programming: From Problem Analysis to Program Design, Second Edition 43

The class Int. Class Java Programming: From Problem Analysis to Program Design, Second Edition The class Int. Class Java Programming: From Problem Analysis to Program Design, Second Edition 44

Chapter Summary s Every GUI contains a window. s Various components are added to 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. Filed 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: From Problem Analysis to Program Design, Second Edition 45

Chapter Summary s A class is a collection of data members and methods associated Chapter Summary 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: From Problem Analysis to Program Design, Second Edition 46