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 Tutorial 4 – Completing the Inventory Application.

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application."— Presentation transcript:

1 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application Introducing Programming Outline 4.1 Test-Driving the Inventory Application 4.2 Introduction to Java Code 4.3 Placing Code in an Event Handler 4.4Performing a Calculation and Displaying the Result 4.5 Wrap-Up

2 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Enable your applications to perform actions in response to JButton clicks. –Use the multiplication operator. –Use method Integer.parseInt to convert a String to an int. –Use method String.valueOf to convert a numeric value to a String.

3 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 4.1 Completing the Inventory Application

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 4.1 Completing the Inventory Application (Cont.) Figure 4.1 Inventory application with quantities entered. Input data into application –Enter 3 in the Cartons per shipment: JTextField –Enter 15 in the Items per carton: JTextField

5 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 4.1 Completing the Inventory Application (Cont.) Figure 4.2 Calculating the total in the Inventory application. Result of calculation Calculating the total –Click the Calculate Total JButton

6 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 4.2 Introduction to Java Code Figure 4.3 Text editor showing a portion of the code for the Inventory application. Beginning of class declaration

7 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 4.2 Introduction to Java Code (Cont.) Java Code –Classes (Case sensitive) Class declaration Class keyword Class name Identifier Left brace Body Right brace Inherits Extends –Methods Blocks –Keywords (reserved words)

8 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 4.3 Placing Code in an Event Handler (Cont.) Figure 4.4 Empty event handler calculateJButtonActionPerformed before you add your application code. Empty event handler Clicking a JButton generates an actionPerformed event –Code in the event handler is executed when JButton is clicked

9 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 4.3 Placing Code in an Event Handler (Cont.) Figure 4.5 Running the application before adding functionality to the event handler.

10 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 4.3 Placing Code in an Event Handler (Cont.) Comment –Indicated by two forward slash characters ( // ) –Full-line comments –End-of-line comments Statement –Ends with a semicolon –Example: totalResultJTextField.setText( “100” ); Method –Called –Return value –Arguments –Dot separator

11 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 4.3 Placing Code in an Event Handler (Cont.) Multiplication operator –Operands Left operand Right operand –Binary operator

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 4.3 Placing Code in an Event Handler (Cont.) Figure 4.6 Code added to the Calculate Total JButton ’s event handler. Event handler for Calculate Total JButton Type this code

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 4.3 Placing Code in an Event Handler (Cont.) Figure 4.7 Execution of application with an event handler. Result of clicking Calculate Total JButton Clicking the Calculate Total JButton –Total: JTextField still displays 45

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 4.4 Performing a Calculation and Displaying the Result Multiline statement –Java ignores extra spaces, tabs and newlines (blank lines) White space Integer.parseInt –Converts a String to an int so that you can perform calculations getText –Returns a String containing the text property of a component

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 4.4 Performing a Calculation and Displaying the Result (Cont.) Figure 4.8 Using multiplication in the Inventory application. Read the values from cartonsJTextField and itemsJTextField, convert them to integers, multiply the integer values and display the result in totalResultJTextField

16 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 4.4 Performing a Calculation and Displaying the Result (Cont.) Figure 4.9 Execution of the completed Inventory application. Result of clicking Calculate Total JButton

17  2004 Prentice Hall, Inc. All rights reserved. Outline 17 Inventory.java (1 of 6) 1 // Tutorial 4: Inventory.java 2 // Calculates the number of items in a shipment based on the number 3 // of cartons received and the number of items per carton. 4 import java.awt.*; 5 import java.awt.event.*; 6 import javax.swing.*; 7 8 public class Inventory extends JFrame 9 { 10 // JLabel and JTextField for cartons per shipment 11 private JLabel cartonsJLabel; 12 private JTextField cartonsJTextField; 13 14 // JLabel and JTextField for items per carton 15 private JLabel itemsJLabel; 16 private JTextField itemsJTextField; 17 18 // JLabel and JTextField for total items per shipment 19 private JLabel totalJLabel; 20 private JTextField totalResultJTextField; 21 22 // JButton to initiate calculation of total items per shipment 23 private JButton calculateJButton; 24

18  2004 Prentice Hall, Inc. All rights reserved. Outline 18 Inventory.java (2 of 6) 25 // no-argument constructor 26 public Inventory() 27 { 28 createUserInterface(); 29 } 30 31 // create and position GUI components; register event handlers 32 public void createUserInterface() 33 { 34 // get content pane and set layout to null 35 Container contentPane = getContentPane(); 36 contentPane.setLayout( null ); 37 38 // set up cartonsJLabel 39 cartonsJLabel = new JLabel(); 40 cartonsJLabel.setText( "Cartons per shipment:" ); 41 cartonsJLabel.setBounds( 16, 16, 130, 21 ); 42 contentPane.add( cartonsJLabel ); 43 44 // set up itemsJLabel 45 itemsJLabel = new JLabel(); 46 itemsJLabel.setText( "Items per carton:" ); 47 itemsJLabel.setBounds( 16, 48, 104, 21 ); 48 contentPane.add( itemsJLabel ); 49

19  2004 Prentice Hall, Inc. All rights reserved. Outline 19 Inventory.java (3 of 6) 50 // set up totalJLabel 51 totalJLabel = new JLabel(); 52 totalJLabel.setText( "Total:" ); 53 totalJLabel.setBounds( 204, 16, 40, 21 ); 54 contentPane.add( totalJLabel ); 55 56 // set up cartonsJTextField 57 cartonsJTextField = new JTextField(); 58 cartonsJTextField.setText( "0" ); 59 cartonsJTextField.setBounds( 148, 16, 40, 21 ); 60 cartonsJTextField.setHorizontalAlignment( JTextField.RIGHT ); 61 contentPane.add( cartonsJTextField ); 62 63 // set up itemsJTextField 64 itemsJTextField = new JTextField(); 65 itemsJTextField.setText( "0" ); 66 itemsJTextField.setBounds( 148, 48, 40, 21 ); 67 itemsJTextField.setHorizontalAlignment( JTextField.RIGHT ); 68 contentPane.add( itemsJTextField ); 69 70 // set up totalResultJTextField 71 totalResultJTextField = new JTextField(); 72 totalResultJTextField.setBounds( 244, 16, 86, 21 ); 73 totalResultJTextField.setHorizontalAlignment( 74 JTextField.RIGHT );

20  2004 Prentice Hall, Inc. All rights reserved. Outline 20 Inventory.java (4 of 6) 75 totalResultJTextField.setEditable( false ); 76 contentPane.add( totalResultJTextField ); 77 78 // set up calculateJButton 79 calculateJButton = new JButton(); 80 calculateJButton.setText( "Calculate Total" ); 81 calculateJButton.setBounds( 204, 48, 126, 24 ); 82 contentPane.add( calculateJButton ); 83 calculateJButton.addActionListener( 84 85 new ActionListener() // anonymous inner class 86 { 87 // method called when calculate JButton is pressed 88 public void actionPerformed( ActionEvent event ) 89 { 90 calculateJButtonActionPerformed( event ); 91 } 92 93 } // end anonymous inner class 94 95 ); // end call to addActionListener 96

21  2004 Prentice Hall, Inc. All rights reserved. Outline 21 Inventory.java (5 of 6) 97 // set properties of window 98 setSize( 354, 112 ); // set window size 99 setTitle( "Inventory" ); // set title bar string 100 setVisible( true ); // display window 101 102 } // end method createUserInterface 103 104 // calculate the total items in the shipment 105 private void calculateJButtonActionPerformed( ActionEvent event ) 106 { 107 // multiply values input and display result in the text field 108 totalResultJTextField.setText( String.valueOf( 109 Integer.parseInt( cartonsJTextField.getText() ) * 110 Integer.parseInt( itemsJTextField.getText() ) ) ); 111 112 } // end method calculateJButtonActionPerformed 113 Read the values from cartonsJTextField and itemsJTextField, convert them to integers, multiply the integer values and display the result in totalResultJText Field

22  2004 Prentice Hall, Inc. All rights reserved. Outline 22 Inventory.java (6 of 6) 114 // main method 115 public static void main( String[] args ) 116 { 117 Inventory application = new Inventory(); 118 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 119 120 } // end method main 121 122 } // end class Inventory


Download ppt "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application."

Similar presentations


Ads by Google