chap06
TRANSCRIPT
![Page 1: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/1.jpg)
Java Programming: From Problem Analysis to Program Design, 3e
Chapter 6
Graphical User Interface (GUI) and Object-Oriented Design (OOD)
![Page 2: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/2.jpg)
2Java Programming: From Problem Analysis to Program Design, 3e
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
![Page 3: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/3.jpg)
3Java Programming: From Problem Analysis to Program Design, 3e
Chapter Objectives
• Discover events and event handlers
• Explore object-oriented design
• Learn how to identify objects, classes, and members of a class
![Page 4: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/4.jpg)
4Java Programming: From Problem Analysis to Program Design, 3e
Graphical User Interface (GUI) Components
• View inputs and outputs simultaneously
• One graphical window
• Input values in any order
• Change input values in window
• Click on buttons to get output
![Page 5: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/5.jpg)
5Java Programming: From Problem Analysis to Program Design, 3e
Java GUI Components
![Page 6: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/6.jpg)
6Java Programming: From Problem Analysis to Program Design, 3e
Graphical User Interface (GUI) Components (continued)
• GUI components placed in content pane
• GUI components– Windows– Labels– Text areas– Buttons
![Page 7: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/7.jpg)
7Java Programming: From Problem Analysis to Program Design, 3e
GUI Components
• Added to content pane of window
• Not added to window itself
• Pixel: picture element
![Page 8: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/8.jpg)
8Java Programming: From Problem Analysis to Program Design, 3e
Windows
• Can be created using a JFrame object• The class JFrame provides various
methods to control attributes of a window• Measured in pixels of height and width• Attributes associated with windows
– Title– Width– Height
![Page 9: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/9.jpg)
9Java Programming: From Problem Analysis to Program Design, 3e
class JFrame
• GUI window instance created as instance of JFrame
• Provides various methods to control window attributes
![Page 10: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/10.jpg)
10Java Programming: From Problem Analysis to Program Design, 3e
Methods Provided by the class JFrame
![Page 11: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/11.jpg)
11Java Programming: From Problem Analysis to Program Design, 3e
Methods Provided by the class Jframe (continued)
![Page 12: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/12.jpg)
12Java Programming: From Problem Analysis to Program Design, 3e
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– Utilizes mechanism of inheritance
![Page 13: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/13.jpg)
13Java Programming: From Problem Analysis to Program Design, 3e
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
![Page 14: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/14.jpg)
14Java Programming: From Problem Analysis to Program Design, 3e
Methods Provided by the class Container
![Page 15: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/15.jpg)
15Java Programming: From Problem Analysis to Program Design, 3e
class JLabel
• Labels: objects of 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
![Page 16: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/16.jpg)
16Java Programming: From Problem Analysis to Program Design, 3e
class Jlabel (continued)
![Page 17: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/17.jpg)
17Java Programming: From Problem Analysis to Program Design, 3e
class JTextField
• Text fields: objects belonging to class JTextField
• To create text field– Declare reference variable of type JTextField
– Instantiate object
![Page 18: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/18.jpg)
18Java Programming: From Problem Analysis to Program Design, 3e
class JTextField (continued)
![Page 19: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/19.jpg)
19Java Programming: From Problem Analysis to Program Design, 3e
class JTextField (continued)
![Page 20: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/20.jpg)
20Java Programming: From Problem Analysis to Program Design, 3e
class JButton
• Provided to create buttons in Java
• To create button– Same technique as creating JLabel and JTextField
![Page 21: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/21.jpg)
21Java Programming: From Problem Analysis to Program Design, 3e
class Jbutton (continued)
![Page 22: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/22.jpg)
22Java Programming: From Problem Analysis to Program Design, 3e
Handling an Event
• Action event: event created when JButton is clicked
• Event listener: object that receives message when JButton is clicked
• In Java, you must register the listener
![Page 23: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/23.jpg)
23Java Programming: From Problem Analysis to Program Design, 3e
Handling an Event (continued)
• class ActionListener– Handles action event – Part of package java.awt.Event– The class ActionListener is a special
type of class (interface)– Must contain actionPerformed method
![Page 24: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/24.jpg)
24Java Programming: From Problem Analysis to Program Design, 3e
Rectangle Program: Sample Run
![Page 25: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/25.jpg)
25Java Programming: From Problem Analysis to Program Design, 3e
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
![Page 26: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/26.jpg)
26Java Programming: From Problem Analysis to Program Design, 3e
Programming Example: Temperature Conversion (continued)
• 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
![Page 27: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/27.jpg)
27Java Programming: From Problem Analysis to Program Design, 3e
Sample Run for TempConversion
![Page 28: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/28.jpg)
28Java Programming: From Problem Analysis to Program Design, 3e
Object-Oriented Design
• Simplified methodology1. Write down detailed description of problem
2. Identify all (relevant) nouns and verbs
3. From list of nouns, select objects
4. Identify data components of each object
5. From list of verbs, select operations
![Page 29: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/29.jpg)
29Java Programming: From Problem Analysis to Program Design, 3e
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
![Page 30: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/30.jpg)
30Java Programming: From Problem Analysis to Program Design, 3e
class Rectangle with Data Members and Operations
![Page 31: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/31.jpg)
31Java Programming: From Problem Analysis to Program Design, 3e
Object-Oriented Design Example 2
• An inoperable candy machine has a cash register and four dispensers to hold and release items sold by the machine
• The machine sells: candies, chips, gum, and cookies
• Write a program for this candy machine so that it can be put into operation
![Page 32: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/32.jpg)
32Java Programming: From Problem Analysis to Program Design, 3e
Object-Oriented Design Example 2 (continued)
• 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
![Page 33: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/33.jpg)
33Java Programming: From Problem Analysis to Program Design, 3e
Object-Oriented Design Example 2 (continued)
![Page 34: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/34.jpg)
34Java Programming: From Problem Analysis to Program Design, 3e
Object-Oriented Design Example 2 (continued)
![Page 35: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/35.jpg)
35Java Programming: From Problem Analysis to Program Design, 3e
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)
![Page 36: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/36.jpg)
36Java Programming: From Problem Analysis to Program Design, 3e
The class Integer
![Page 37: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/37.jpg)
37Java Programming: From Problem Analysis to Program Design, 3e
The class Integer (continued)
![Page 38: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/38.jpg)
38Java Programming: From Problem Analysis to Program Design, 3e
The class Integer (continued)
![Page 39: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/39.jpg)
39Java Programming: From Problem Analysis to Program Design, 3e
The class Integer (continued)
Integer num; num = new Integer(86)
![Page 40: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/40.jpg)
40Java Programming: From Problem Analysis to Program Design, 3e
The class Integer (continued)
int x; Integer num;
num = 25;
For the most part, this statement is similar to the statement:
num = new Integer(25);
The expression:
num = 25;
is referred to as autoboxing of the int type
![Page 41: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/41.jpg)
41Java Programming: From Problem Analysis to Program Design, 3e
The class Integer (continued)
int x;
Integer num;
The statement:
x = num;
This statement is equivalent to the statement:
x = num.intValue();
This statement is referred to as auto unboxing of the int type
![Page 42: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/42.jpg)
42Java Programming: From Problem Analysis to Program Design, 3e
The class Integer (continued)
• To compare the values of two Integer objects, you can use the method compareTo
• If you want to compare the values of two Integer objects only for equality, then you can use the method equals
![Page 43: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/43.jpg)
43Java Programming: From Problem Analysis to Program Design, 3e
The class Integer (continued)
![Page 44: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/44.jpg)
44Java Programming: From Problem Analysis to Program Design, 3e
The class Integer (continued)
![Page 45: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/45.jpg)
45Java Programming: From Problem Analysis to Program Design, 3e
Chapter Summary• Every GUI contains a window• Various components are added to content pane of
window• class JFrame is used to create windows• JLabel is used to label GUI components and
display information to user• JTextFiled is used for input/output• JButton generates action event• Action event is sent to action listener
![Page 46: Chap06](https://reader035.vdocuments.us/reader035/viewer/2022062405/555c3f18d8b42a2c068b4c61/html5/thumbnails/46.jpg)
46Java Programming: From Problem Analysis to Program Design, 3e
Chapter Summary (continued)• Action listener must have method called actionPerformed
• class: collection of data members and methods associated with those members
• Object-Oriented Design (OOD)– Starts with a problem statement– Identifies classes required with nouns in problem statement– Identifies methods required with verbs in problem
specification