Download presentation
Presentation is loading. Please wait.
Published byMaryann Jordan Modified over 9 years ago
1
Java Applet Basics (2)
2
The Body Mass Index Calculator
3
The Applet Skeleton import java.applet.*; import java.awt.*; public class BodyMassApplet extends Applet { }
4
The HTML Page
5
Create the Skeleton Files
6
Control Components Label TextField Button
7
Add a Label Control import java.applet.*; import java.awt.*; public class BodyMassApplet extends Applet { Label myLabel = new Label(“XXXXXXXX”); public void init() { add(myLabel); }
8
Member Variable
9
Add Labels into the Skeleton
10
Add a TextField Control import java.applet.*; import java.awt.*; public class BodyMassApplet extends Applet { TextField myTextField = new TextField(columns); public void init() { add(myTextField); }
11
Add TextFields into the Skeleton
12
Add a Button Control import java.applet.*; import java.awt.*; public class BodyMassApplet extends Applet { Button myButton = new Button(“Text on the Button”); public void init() { add(myButton); }
13
Add Buttons into the Skeleton
14
Handle Action Events import 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 }
15
Add the Event Handler
16
Display Text on a Label Control Syntax: myLabel.setText(“the text shown on the label control”);
17
Read Text from a TextField Syntax: myTextField.getText(); E.g. String input; input = myTextField.getText();
18
Read Inputs From all TextFields
19
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?
20
Round the Body Index
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.