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 Class2 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
Chapter 11 - A GUI Interacting With a Problem Domain Class3 Adding a New Customer
Chapter 11 - A GUI Interacting With a Problem Domain Class4 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
Chapter 11 - A GUI Interacting With a Problem Domain Class5 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
Chapter 11 - A GUI Interacting With a Problem Domain Class6 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
Chapter 11 - A GUI Interacting With a Problem Domain Class7 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
Chapter 11 - A GUI Interacting With a Problem Domain Class8 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
Chapter 11 - A GUI Interacting With a Problem Domain Class9 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
Chapter 11 - A GUI Interacting With a Problem Domain Class10 Using a Dialog Box
Chapter 11 - A GUI Interacting With a Problem Domain Class11 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
Chapter 11 - A GUI Interacting With a Problem Domain Class12 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
Chapter 11 - A GUI Interacting With a Problem Domain Class13 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
Chapter 11 - A GUI Interacting With a Problem Domain Class14 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
Chapter 11 - A GUI Interacting With a Problem Domain Class15 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
Chapter 11 - A GUI Interacting With a Problem Domain Class16 Adding a Boat 11
Chapter 11 - A GUI Interacting With a Problem Domain Class17 Adding a Boat 11
Chapter 11 - A GUI Interacting With a Problem Domain Class18 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
Chapter 11 - A GUI Interacting With a Problem Domain Class19 Add A Sailboat 11
Chapter 11 - A GUI Interacting With a Problem Domain Class20 Add A PowerBoat
Chapter 11 - A GUI Interacting With a Problem Domain Class21 The clearForm Method To blank out the text fields use the setText method – pass an empty string as the argument: manufacturerText.setText(“”); 11