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 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, Second Edition 10

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 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

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: 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, Second Edition 21

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 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 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 25

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. 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 Edition 28

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 Design, Second Edition 31

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 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

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 38

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 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 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 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 43

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 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 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