21
Chapter 11 - A GUI Interacting With a Proble m Domain Class 1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Embed Size (px)

Citation preview

Page 1: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 1

Chapter 11

A GUI Interacting With a Problem Domain Class

11

Page 2: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 2

1. Adding a New Customer Develop a new GUI class named

AddCustomer to input customer attribute values and then create instances of the Customer class

See Figure 11-4

Page 3: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 3

Adding a New Customer

Page 4: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 4

Adding a New Customer You need to start by importing the classes it uses:

The java.awt class has the Font and Color classes The javax.swing class contains the GUI components The ActionEvent and ActionListener interfaces are in the

java.awt.event class

JTextField instances are used to input the customer’s name, address, and phone number

JButton instances are used after the customer data is entered, to clear the text fields, and to end processing

11

Page 5: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 5

Creating the Logo Panel The JLabel class is used to contain the

marina’s logo The foreground color is set to red using the

Color.red constant The font is set to Times Roman 36 point

italic using the “TimesRoman” font name, and the Font.ITALIC constant

11

Page 6: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 6

Creating the Center Panel The customer name, address, and phone

text boxes are added to the centerPanel This panel is added to the Frame instance The SwingConstants class is used to set the

alignment LEFT RIGHT CENTER

Page 7: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 7

Creating the Lower Panel Three button instances are added to the

lower panel to support the Add, Clear, and Close operations

You need to call the addActionListener method for each button to process the events

11

Page 8: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 8

Handling Events An anonymous inner class is defined to

handle events The actionPerformed method determines

which button was clicked, and then invokes the appropriate method to handle the event

Page 9: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 9

Using a Dialog Box If the user clicks the Add button, but hasn’t

entered valid data, an error message should be displayed

The statement to display a simple dialog box with a message that requires the user to click the OK button to continue is:JOptionPane.showMessageDialog(this,“message”);

11

Page 10: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 10

Using a Dialog Box

Page 11: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 11

2. Finding a Customer (Fig. 11-6) The FindCustomer class will allow a user to find

a customer and then display their address and phone number

Page 12: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 12

Creating a Vector of Customers Two vector variables are used to hold a list

of customers and their names The add method is used to add Customer

objects to the vector

11

Page 13: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 13

Using the JList Class The JList class is used to display a list of

customer names When the user selects a name and then

clicks on the Find button, that customer’s name and address are displayed

The JList constructor takes a Vector object which contains the data that is displayed

Page 14: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 14

Adding Text Fields JTextField objects are created to display the

address and phone number A JTextField object is used to allow the user

to update these values

Page 15: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 15

3. Adding a Boat Develop a GUI that allows you to add a new

boat to the marina system You can add a PowerBoat or SailBoat See Figure 11-13

Page 16: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 16

Adding a Boat11

Page 17: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 17

Adding a Boat

11

Page 18: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 18

Using Radio Buttons The radio buttons are used to indicate the

type of boat you are adding The RadioButton constructor takes two

arguments The caption to be displayed The buttons initial state – true if selected, false

otherwise

You can trigger a radio button click event by calling doClick

Page 19: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 19

Add A Sailboat11

Page 20: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 20

Add A PowerBoat

Page 21: Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11

Chapter 11 - A GUI Interacting With a Problem Domain Class 21

The clearForm Method To blank out the text fields use the setText

method – pass an empty string as the argument: manufacturerText.setText(“”);

11