Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.

Similar presentations


Presentation on theme: "Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet."— Presentation transcript:

1 Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet

2 Chapter 3: Manipulating Data Using Methods 2 Chapter Objectives Implement an ActionListener to handle events Add interface components to an applet Use the init() and paint() methods to load the applet interface Use the actionPerformed() method Run and test an interactive applet

3 Chapter 3: Manipulating Data Using Methods 3 Moving to the Web Create a new Java Applet in NetBeans: –Right click on the bodymasscalculator package and select New > Java > Applet –Name the Applet BodyMassApplet

4 Chapter 3: Manipulating Data Using Methods 4 Moving to the Web The applet version of the Body Mass Index Calculator has four kinds of objects – Image, Labels, TextFields, and Buttons Import three packages –Java.applet.* –Java.awt.* –Java.awt.event.* Implement an ActionListener interface in the class header –Informs the program to respond to user-driven events

5 Chapter 3: Manipulating Data Using Methods 5 Moving to the Web

6 Chapter 3: Manipulating Data Using Methods 6 Moving to the Web Every event class has one or more associated listener interfaces

7 Chapter 3: Manipulating Data Using Methods 7 Adding Interface Components to an Applet Label –Displays text in the applet window TextField –Displays a text box for users to enter text Buttons –Displays a command button for users to click

8 Chapter 3: Manipulating Data Using Methods 8 Label and Textfield Declarations Construct the components to be displayed in the applet

9 Chapter 3: Manipulating Data Using Methods 9 The init() Method Initializes the window color and graphic Adds components to the applet window Registers the Button’s ActionListener

10 Chapter 3: Manipulating Data Using Methods 10 The actionPerformed() Method When a click event occurs, the ActionListener’s actionPerformed() method is triggered –Input values are retrieved with getText() –Calculations are performed –Output is sent to a label with setText()

11 Chapter 3: Manipulating Data Using Methods 11 The paint() Method Draws the initialized image in the applet window

12 Chapter 3: Manipulating Data Using Methods 12 Creating an HTML Host Document for an Interactive Applet Compile the applet NetBeans creates an HTML Host Document to execute the applet –Uses the tag to specify the bytecode file, and width and height of the window (NetBeans uses defaults for width and height) Use the same sample data to test the applet

13 Chapter 3: Manipulating Data Using Methods 13 Summary Applet code is controlled by first defining Labels, TextFields and Buttons Use the add method to place the items on in the web document Create an actionPerformed method to be executed when a button is clicked

14 Chapter 3: Manipulating Data Using Methods 14 Rest of Today Complete a Java Applet for the body mass calculator –Full list of the code can be found on page 190 of the book Once built copy the logo.gif file to the “build” folder, edit the HTML file to change the height to 500 and open the HTML file.


Download ppt "Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet."

Similar presentations


Ads by Google