20
Java Applet Basics (2)

Java Applet Basics (2). The Body Mass Index Calculator

Embed Size (px)

Citation preview

Page 1: Java Applet Basics (2). The Body Mass Index Calculator

Java Applet Basics (2)

Page 2: Java Applet Basics (2). The Body Mass Index Calculator

The Body Mass Index Calculator

Page 3: Java Applet Basics (2). The Body Mass Index Calculator

The Applet Skeleton

import java.applet.*;import java.awt.*;

public class BodyMassApplet extends Applet{}

Page 4: Java Applet Basics (2). The Body Mass Index Calculator

The HTML Page

<HTML><APPLET CODE = “XXXX.class" WIDTH=XXX HEIGHT = XXX></APPLET></HTML>

Page 5: Java Applet Basics (2). The Body Mass Index Calculator

Create the Skeleton Files

Page 6: Java Applet Basics (2). The Body Mass Index Calculator

Control Components

• Label

• TextField

• Button

Page 7: Java Applet Basics (2). The Body Mass Index Calculator

Add a Label Controlimport java.applet.*;import java.awt.*;

public class BodyMassApplet extends Applet{ Label myLabel = new Label(“XXXXXXXX”); public void init() { add(myLabel); }}

Page 8: Java Applet Basics (2). The Body Mass Index Calculator

Member Variable

Page 9: Java Applet Basics (2). The Body Mass Index Calculator

Add Labels into the Skeleton

Page 10: Java Applet Basics (2). The Body Mass Index Calculator

Add a TextField Controlimport java.applet.*;import java.awt.*;

public class BodyMassApplet extends Applet{ TextField myTextField = new TextField(columns); public void init() { add(myTextField); }}

Page 11: Java Applet Basics (2). The Body Mass Index Calculator

Add TextFields into the Skeleton

Page 12: Java Applet Basics (2). The Body Mass Index Calculator

Add a Button Controlimport java.applet.*;import java.awt.*;

public class BodyMassApplet extends Applet{ Button myButton = new Button(“Text on the Button”); public void init() { add(myButton); }}

Page 13: Java Applet Basics (2). The Body Mass Index Calculator

Add Buttons into the Skeleton

Page 14: Java Applet Basics (2). The Body Mass Index Calculator

Handle Action Eventsimport java.awt.event.*;

public class BodyMassApplet extends Applet implements ActionListener{ Button myButton = new Button(“my button”); public void init() { add(myButton); myButton.addActionListener(this); }

public void actionPerformed(ActionEvent e) { //handle the event here }}

Page 15: Java Applet Basics (2). The Body Mass Index Calculator

Add the Event Handler

Page 16: Java Applet Basics (2). The Body Mass Index Calculator

Display Text on a Label Control

• Syntax: myLabel.setText(“the text shown on the label control”);

Page 17: Java Applet Basics (2). The Body Mass Index Calculator

Read Text from a TextField

• Syntax: myTextField.getText();

• E.g.String input;

input = myTextField.getText();

Page 18: Java Applet Basics (2). The Body Mass Index Calculator

Read Inputs From all TextFields

Page 19: Java Applet Basics (2). The Body Mass Index Calculator

Math.round(dbl)

• Rounds a real number into the nearest integer

• Syntax: integer = Math.round(dbl)• Example:

double dblIndex;int intIndex;intIndex = Math.round(dblIndex);

• Question: what’s the difference between Math.round(dbl) and (int)dbl?

Page 20: Java Applet Basics (2). The Body Mass Index Calculator

Round the Body Index