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

Slides:



Advertisements
Similar presentations
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Advertisements

Drawing in a frame – Java GUI
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Outline DivideByZeroTes t.java 1 // Fig. 15.1: DivideByZeroTest.java 2 // An exception-handling example that checks for divide-by-zero. 3 import java.awt.*;
 2003 Prentice Hall, Inc. All rights reserved. Chapter 15 – Exception Handling Outline 15.1 Introduction 15.2 Exception-Handling Overview 15.3 Exception-Handling.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 15.1 Test-Driving the Craps Game Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 14.1 Test-Driving the Fund Raiser Application.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Chapter 121 Window Interfaces Using Swing Chapter 12.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Objectives –Display a date and time. –Use a JPasswordField.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 6.1 Test-Driving the Wage Calculator Application.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Copyright © Curt Hill First Window Builder Program Easy GUIs in Eclipse.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 18.1 Test-Driving the Microwave Oven Application.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 5 Completing the Inventory Application Introducing Programming.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 25 – Ticket Information Application Introducing.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 22.1 Test-Driving the Typing Skills Developer.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Enhancing the Wage Calculator Application.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Inventory Application.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 11.1 Test-Driving the Security Panel Application.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 13.1 Test-Driving the Enhanced Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 25 - Beyond C & C++: Operators, Methods, and.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CompSci 230 S Programming Techniques
Chapter 14 – Exception Handling
A First Look at GUI Applications
CiS 260: App Dev I Chapter 6: GUI and OOD.
Chapter 15 – Exception Handling
Presentation transcript:

© Copyright 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

© Copyright 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.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Completing the Inventory Application

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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)

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

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

 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; // JLabel and JTextField for items per carton 15 private JLabel itemsJLabel; 16 private JTextField itemsJTextField; // JLabel and JTextField for total items per shipment 19 private JLabel totalJLabel; 20 private JTextField totalResultJTextField; // JButton to initiate calculation of total items per shipment 23 private JButton calculateJButton; 24

 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 } // 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 ); // set up cartonsJLabel 39 cartonsJLabel = new JLabel(); 40 cartonsJLabel.setText( "Cartons per shipment:" ); 41 cartonsJLabel.setBounds( 16, 16, 130, 21 ); 42 contentPane.add( cartonsJLabel ); // 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

 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 ); // 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 ); // 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 ); // set up totalResultJTextField 71 totalResultJTextField = new JTextField(); 72 totalResultJTextField.setBounds( 244, 16, 86, 21 ); 73 totalResultJTextField.setHorizontalAlignment( 74 JTextField.RIGHT );

 2004 Prentice Hall, Inc. All rights reserved. Outline 20 Inventory.java (4 of 6) 75 totalResultJTextField.setEditable( false ); 76 contentPane.add( totalResultJTextField ); // 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( 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 } } // end anonymous inner class ); // end call to addActionListener 96

 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 } // end method createUserInterface // 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() ) ) ); } // 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

 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 ); } // end method main } // end class Inventory