1 / 14

Chapter 3

Chapter 3. Section 65 – Manipulating Data Using Methods – Java Applet. 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

oleg-talley
Télécharger la présentation

Chapter 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet

  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. 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. 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. Moving to the Web

  6. Moving to the Web • Every event class has one or more associated listener interfaces

  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. Label and Textfield Declarations • Construct the components to be displayed in the applet

  9. The init() Method • Initializes the window color and graphic • Adds components to the applet window • Registers the Button’s ActionListener

  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. The paint() Method • Draws the initialized image in the applet window

  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 <APPLET> 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. 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. 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.

More Related