Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application."— Presentation transcript:

1 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application 7.2 Constructing the Dental Payment Application 7.3Using JCheckBoxes 7.4Using a Dialog to Display a Message 7.5Logical Operators 7.6Wrap-Up Tutorial 7 – Dental Payment Application Introducing JCheckBox es and Message Dialogs

2 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Use JCheckBox es to allow users to select options. –Use dialogs to display messages. –Use logical operators to form more complex conditions.

3 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 7.1 Test-Driving the Dental Payment Application

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 7.1 Test-Driving the Dental Payment Application (Cont.) Figure 7.1 Dental Payment application without input entered. JCheckBox components (unchecked)

5 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 Figure 7.2 Message dialog appears when no name is entered and/or no JCheckBox es are selected. 7.1 Test-Driving the Dental Payment Application (Cont.) Message dialog Run the Dental Payment application – cd c:\Examples\Tutorial07\ CompletedApplication\DentalPayment – java DentalPayment Click OK JButton to close the dialog

6 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 7.1 Test-Driving the Dental Payment Application (Cont.) Figure 7.3 Dental Payment application with input entered and total price of services displayed. JCheckBox components (checked)

7 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 7.2 Constructing the Dental Payment Application When the user clicks the Calculate JButton: Input patient name If user has not entered a patient name or has not selected any JCheckBoxes Display error message in dialog Else If Cleaning JCheckBox is selected Add cost of a cleaning to the total If Cavity Filling JCheckBox is selected Add cost of a cavity filling to the total If X-Ray JCheckBox is selected Add cost of an x-ray to the total Display total price of services rendered in dollar format

8 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 7.2 Constructing the Dental Payment Application

9 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 7.3 Using JCheckBox es JCheckBox es – small gray square either blank or containing a small checkmark – Properties: – selected property: whether or not a JCheckBox is selected – text property: the text just beside a JCheckBox – bounds property: location of JCheckBox – Methods: – isSelected – returns true if JCheckBox is selected – setText – sets text of JCheckBox – setBounds – sets bounds of JCheckBox state button: a button that has either an on or off state

10 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 7.3 Using JCheckBox es (Cont.) Figure 7.5 Changing the text and bounds properties of cleaningJCheckBox. Use methods setBounds and setText to change JCheckBox properties

11 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 7.3 Using JCheckBox es (Cont.) Figure 7.6 Changing text and bounds properties of cavityFillingJCheckBox.

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 7.3 Using JCheckBox es (Cont.) Figure 7.7 Changing text and bounds properties of xRayJCheckBox.

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 7.3 Using JCheckBox es (Cont.) Figure 7.8 Application running after customizing three JCheckBox es.

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 7.3 Using JCheckBox es (Cont.) Use method isSelected to determine if a JCheckBox is selected.

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 7.3 Using JCheckBox es (Cont.) Figure 7.9 Determining if a JCheckBox has been selected. Calling method isSelected Add this code to add the proper amount to total

16 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 7.3 Using JCheckBox es (Cont.) Figure 7.10 Application running without input. Application calculates a bill of $0.00 when no JCheckBox es are checked

17 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 7.3 Using JCheckBox es (Cont.) Figure 7.11 Application running with services selected, but no patient name entered.

18 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 7.4 Using a Dialog to Display a Message Figure 7.12 Message dialog displayed by the application. Close buttonDialog sized to accommodate contents Title barIcon indicates the tone of the message OK JButton allows user to close the dialog

19 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 7.4 Using a Dialog to Display a Message (Cont.) Figure 7.13 Adding an if statement to event handler calculateJButtonActionPerformed. Add this code to verify patient name was entered

20 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 7.4 Using a Dialog to Display a Message (Cont.) Figure 7.14 Message dialog code that displays a message to users. Add an error message Use method showMessageDialog to display error message in separate window

21 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21 7.4 Using a Dialog to Display a Message (Cont.) Figure 7.15 Adding an else part to an if statement. Add an else statement Add the right brace to end else statement

22 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 7.4 Using a Dialog to Display a Message (Cont.) Figure 7.17 Application running without a name entered.

23 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23 7.4 Using a Dialog to Display a Message (Cont.) Figure 7.18 Application running with a name entered, but without any JCheckBox es selected. User must now enter a nameApplication calculates a bill of $0.00 when a name is entered and no JCheckBox es are checked

24 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 7.4 Using a Dialog to Display a Message (Cont.)

25 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 7.5 Logical Operators Logical operators –&& (conditional AND) –|| (conditional OR) –^ (boolean logical exclusive OR) –! (logical negation)

26 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 7.5 Logical Operators (Cont.) Using Conditional AND ( && ) if ( gender.equals( "Female" ) && age >= 65 ) { seniorFemales += 1; }

27 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27 7.5 Logical Operators (Cont.) Using Conditional OR ( || ) if ( semesterAverage >= 90 || finalExam >= 90 ) { JOptionPane.showMessageDialog( null, "Student grade is A.", "Student Grade", JOptionPane.INFORMATION_MESSAGE ); }

28 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 28 7.5 Logical Operators (Cont.) Using Boolean Logical Exclusive OR ( ^ )

29 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 7.5 Logical Operators (Cont.) Using Logical Negation ( ! ) if ( !( grade == value ) ) { displayLabel.setText( "They are not equal!" ); }

30 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 30 7.5 Logical Operators (Cont.) Figure 7.24 Using the && and || logical operators. Inserting a complex expression

31 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 31 7.5 Logical Operators (Cont.) Figure 7.25 Message dialog displayed if no name is entered and no JCheckBox es are selected.

32 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 32 7.5 Logical Operators (Cont.) Figure 7.26 Message dialog displayed for incorrect input.

33 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 33 7.5 Logical Operators (Cont.) Figure 7.27 Running the completed application with correct input.

34  2004 Prentice Hall, Inc. All rights reserved. Outline 34 DentalPayment.java (1 of 10) 1 // Tutorial 7: DentalPayment.java 2 // This program calculates the total cost of the bill for a 3 // patient at a dental office. 4 import java.awt.*; 5 import java.awt.event.*; 6 import java.text.*; 7 import javax.swing.*; 8 9 public class DentalPayment extends JFrame 10 { 11 // JLabel that displays header on application window 12 private JLabel dentalPaymentFormJLabel; 13 14 // JLabel and JTextField for patient name 15 private JLabel patientNameJLabel; 16 private JTextField patientNameJTextField; 17 18 // JCheckBox and JLabel for cleaning 19 private JCheckBox cleaningJCheckBox; 20 private JLabel cleaningPriceJLabel; 21 22 // JCheckBox and JLabel for cavity filling 23 private JCheckBox cavityFillingJCheckBox; 24 private JLabel cavityFillingPriceJLabel; 25

35  2004 Prentice Hall, Inc. All rights reserved. Outline 35 DentalPayment.java (2 of 10) 26 // JCheckBox and JLabel for X-Ray 27 private JCheckBox xRayJCheckBox; 28 private JLabel xRayPriceJLabel; 29 30 // JLabel and JTextField for total fee 31 private JLabel totalJLabel; 32 private JTextField totalJTextField; 33 34 // JButton to initiate calculation of fee 35 private JButton calculateJButton; 36 37 // no-argument constructor 38 public DentalPayment() 39 { 40 createUserInterface(); 41 } 42 43 // create and position GUI components; register event handlers 44 private void createUserInterface() 45 { 46 // get content pane for attaching GUI components 47 Container contentPane = getContentPane(); 48 49 // enable explicit positioning of GUI components 50 contentPane.setLayout( null );

36  2004 Prentice Hall, Inc. All rights reserved. Outline 36 DentalPayment.java (3 of 10) 51 52 // set up dentalPaymentFormJLabel 53 dentalPaymentFormJLabel = new JLabel(); 54 dentalPaymentFormJLabel.setBounds( 19, 19, 235, 28 ); 55 dentalPaymentFormJLabel.setText( "Dental Payment Form" ); 56 dentalPaymentFormJLabel.setFont( 57 new Font( "Default", Font.PLAIN, 22 ) ); 58 dentalPaymentFormJLabel.setHorizontalAlignment( 59 JLabel.CENTER ); 60 contentPane.add( dentalPaymentFormJLabel ); 61 62 // set up patientNameJLabel 63 patientNameJLabel = new JLabel(); 64 patientNameJLabel.setBounds( 19, 65, 91, 21 ); 65 patientNameJLabel.setText( "Patient name:" ); 66 contentPane.add( patientNameJLabel ); 67 68 // set up patientNameJTextField 69 patientNameJTextField = new JTextField(); 70 patientNameJTextField.setBounds( 132, 65, 117, 21 ); 71 contentPane.add( patientNameJTextField ); 72

37  2004 Prentice Hall, Inc. All rights reserved. Outline 37 DentalPayment.java (4 of 10) 73 // set up cleaningJCheckBox 74 cleaningJCheckBox = new JCheckBox(); 75 cleaningJCheckBox.setBounds( 16, 112, 122, 24 ); 76 cleaningJCheckBox.setText( "Cleaning" ); 77 contentPane.add( cleaningJCheckBox ); 78 79 // set up cleaningPriceJLabel 80 cleaningPriceJLabel = new JLabel(); 81 cleaningPriceJLabel.setBounds( 211, 112, 38, 24 ); 82 cleaningPriceJLabel.setText( "$35" ); 83 cleaningPriceJLabel.setHorizontalAlignment( JLabel.RIGHT ); 84 contentPane.add( cleaningPriceJLabel ); 85 86 // set up cavityFillingJCheckBox 87 cavityFillingJCheckBox = new JCheckBox(); 88 cavityFillingJCheckBox.setBounds( 16, 159, 122, 24 ); 89 cavityFillingJCheckBox.setText( "Cavity Filling" ); 90 contentPane.add( cavityFillingJCheckBox ); 91 Customizing cleaningJCheckBox Customizing cavityFilling JCheckBox

38  2004 Prentice Hall, Inc. All rights reserved. Outline 38 DentalPayment.java (5 of 10) 92 // set up cavityFillingPriceJLabel 93 cavityFillingPriceJLabel = new JLabel(); 94 cavityFillingPriceJLabel.setBounds( 211, 159, 38, 24 ); 95 cavityFillingPriceJLabel.setText( "$150" ); 96 cavityFillingPriceJLabel.setHorizontalAlignment( 97 JLabel.RIGHT ); 98 contentPane.add( cavityFillingPriceJLabel ); 99 100 // set up xRayJCheckBox 101 xRayJCheckBox = new JCheckBox(); 102 xRayJCheckBox.setBounds( 16, 206, 122, 24 ); 103 xRayJCheckBox.setText( "X-Ray" ); 104 contentPane.add( xRayJCheckBox ); 105 106 // set up xRayPriceJLabel 107 xRayPriceJLabel = new JLabel(); 108 xRayPriceJLabel.setBounds( 211, 206, 38, 24 ); 109 xRayPriceJLabel.setText( "$85" ); 110 xRayPriceJLabel.setHorizontalAlignment( JLabel.RIGHT ); 111 contentPane.add( xRayPriceJLabel ); 112 Customizing x RayJCheckBox

39  2004 Prentice Hall, Inc. All rights reserved. Outline 39 DentalPayment.java (6 of 10) 113 // set up totalJLabel 114 totalJLabel = new JLabel(); 115 totalJLabel.setBounds( 144, 256, 41, 21 ); 116 totalJLabel.setText( "Total:" ); 117 contentPane.add( totalJLabel ); 118 119 // set up totalJTextField 120 totalJTextField = new JTextField(); 121 totalJTextField.setBounds( 192, 256, 56, 21 ); 122 totalJTextField.setEditable( false ); 123 totalJTextField.setHorizontalAlignment( JTextField.CENTER ); 124 contentPane.add( totalJTextField ); 125 126 // set up calculateJButton 127 calculateJButton = new JButton(); 128 calculateJButton.setBounds( 155, 296, 94, 24 ); 129 calculateJButton.setText( "Calculate" ); 130 contentPane.add( calculateJButton ); 131 calculateJButton.addActionListener( 132 133 new ActionListener() // anonymous inner class 134 {

40  2004 Prentice Hall, Inc. All rights reserved. Outline 40 DentalPayment.java (7 of 10) 135 // event handler called when user clicks calculateJButton 136 public void actionPerformed( ActionEvent event ) 137 { 138 calculateJButtonActionPerformed( event ); 139 } 140 141 } // end anonymous inner class 142 143 ); // end call to addActionListener 144 145 // set properties of application’s window 146 setTitle( "Dental Payment" ); // set title bar string 147 setSize( 272, 364 ); // set window size 148 setVisible( true ); // display window 149 150 } // end method createUserInterface 151 152 // calculate cost of patient's visit 153 private void calculateJButtonActionPerformed( ActionEvent event ) 154 { 155 // get patient's name 156 String patient = patientNameJTextField.getText(); 157 Obtaining patient name

41  2004 Prentice Hall, Inc. All rights reserved. Outline 41 DentalPayment.java (8 of 10) 158 // display error message if no name entered or no box selected 159 if ( ( patient.equals( "" ) ) || 160 ( !cleaningJCheckBox.isSelected() && 161 !cavityFillingJCheckBox.isSelected() && 162 !xRayJCheckBox.isSelected() ) ) 163 { 164 // display error message 165 JOptionPane.showMessageDialog( null, 166 "Please enter a name and check at least one item.", 167 "Missing information", JOptionPane.WARNING_MESSAGE ); 168 } 169 else // otherwise, do calculations 170 { 171 double total = 0.0; // sum of all services provided 172 173 // if patient had a cleaning 174 if ( cleaningJCheckBox.isSelected() ) 175 { 176 total += 35; // add 35 to total 177 } 178 Using logical operators and JCheckBox method isSelected Displaying a JOptionPane message dialog Examining the selected property of cleaningJCheckBox

42  2004 Prentice Hall, Inc. All rights reserved. Outline 42 DentalPayment.java (9 of 10) 179 // if patient had cavity filled 180 if ( cavityFillingJCheckBox.isSelected() ) 181 { 182 total += 150; // add 150 to total 183 } 184 185 // if patient had x-ray taken 186 if ( xRayJCheckBox.isSelected() ) 187 { 188 total += 85; // add 85 to total 189 } 190 191 // specify display format 192 DecimalFormat dollars = new DecimalFormat( "$0.00" ); 193 194 // display total 195 totalJTextField.setText( dollars.format( total ) ); 196 197 } // end else 198 199 } // end method calculateJButtonActionPerformed 200 Examining the selected property of cavityFillingJCh eckBox Examining the selected property of xRayJCheckBox Format and display total

43  2004 Prentice Hall, Inc. All rights reserved. Outline 43 DentalPayment.java (10 of 10) 201 // main method 202 public static void main( String [] args ) 203 { 204 DentalPayment application = new DentalPayment(); 205 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 206 207 } // end method main 208 209 } // end class DentalPayment


Download ppt "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application."

Similar presentations


Ads by Google