HTML and Applet Basics - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

HTML and Applet Basics

Description:

Open HTML document (AppletViewer, Netscape Navigator, Internet Explorer) HTML Documents ... Font object requires: typeface, style, point size ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 24
Provided by: debbie155
Category:

less

Transcript and Presenter's Notes

Title: HTML and Applet Basics


1
HTML and Applet Basics
  • JAVA - Ch9 part 1
  • Dr. James Jiang

Applets
2
Console Applications vs Applets
Applications are stand-alone programs!
  • Create .java files
  • Build .java files to bytecode (.class files)
  • Execute the .class files
  • Programs called from within Web pages
  • Applets must be called from within HTML documents
  • OR
  • Run AppletViewer

Applets are called within an application
3
Steps to Create an Applet
  • Create .java file with code
  • Design an HTML document that calls your compiled
    Java class
  • Build project to create .class files
  • Open HTML document (AppletViewer, Netscape
    Navigator, Internet Explorer)

4
HTML Documents
  • Text documents with formatting instructions
    (tags)
  • Documents are formatted on Web pages based on tag
    instructions

HTML languages primary purpose is to manage the
display of HTML documents
5
The ltAPPLETgt Tag
  • Used to call a Java applet from a Web page
  • Use attributes to configure the applet

ltAPPLET CODE myApplet.class WIDTH 300
HEIGHT 200gt lt/APPLETgt
Components of the ltAPPLETgt tag
CODE named of the compiled applet WIDTH width
of the applet on the screen HEIGHT height of
the applet on the screen
tip
WIDTH and HEIGHT are pixel measurements
6
TeschTest.html
ltHTMLgt ltAPPLET CODE TeschGreet.class" WIDTH
450 HEIGHT 200gt lt/APPLETgt lt/HTMLgt
7
Writing an Applet
  • Add necessary import statements
  • import java.applet.
  • import java.awt.
  • Abstract Windows Toolkit
  • Contains Windows components
  • controls (labels, buttons, menus)

Contains the Applet class
Abstract Windows Toolkit (awt)
8
Writing an Applet
  • Use appropriate Windows components and applet
    methods
  • Use the Label class to create a label
  • Use the add() method to add the component to the
    window

Label greeting new Label(Hi!)
add(greeting)
9
Writing an Applet
  • Extend your class to include the Applet class
  • public class Greet extends Applet
  • User applets inherit (build upon) traits of the
    Applet class

10
Writing an Applet
  • Every applet includes 4 methods
  • public void init()
  • public void start()
  • public void stop()
  • public void destroy()
  • Java creates these as empty methods if your
    applet doesnt contain them

11
The setFont()Method
  • Requires a Font object argument
  • Font object requires typeface, style, point size

Font hlFont new Font(Helvetica, Font.BOLD,
36) greeting.setFont(hlFont)
12
The TextField Component
  • AKA Textbox
  • User types a single line of text data
  • TextField object Constructors
  • public TextField()
  • public TextField(int NumColumns)
  • public TextField(String initialText)
  • public TextField(String initialText, int
    numColumns)

TextField answer new TextField(10)
13
Other TextField Methods
answer.setText(Thank you)
String whatDidTheySay answer.getText()
answer.requestFocus()
Answer.setEditable(false)
14
The Button Component
  • Constructors
  • public Button()
  • public Button(String label)

Button readyButton new Button(Press when
ready) add(readyButton) readyButton.setLabel(D
ont press me again!) String whatsOnButton
readyButton.getLabel()
15
Event-Driven Programming
  • Event action on a component
  • Actions initiated in any order
  • Source component on which an event is generated
  • Listener object that is interested in an event
  • To make an object a listener, register the object
    as a listener for the source
  • Source objects maintain a list of registered
    listeners

16
To respond to user events in an applet
  • Prepare your applet to accept event messages
  • Tell your applet to expect events to happen
  • Tell your applet how to respond to any events
    that happen

17
Preparing Applets to Accept Event Messages
  • Import java.awt.event package
  • Add implements ActionListener to class header
  • ActionListener is an interface set of
    specifications for methods used with Event objects

18
Telling Your Applet to ExpectEvents to Happen
  • Use the addActionListener() method
  • Consider the applet a target for the message
  • Consider the component (button, textfield) the
    source of the message
  • aButton.addActionListener(this)

Send any ActionEvent messages (button clicks) to
the current applet
19
Telling your Applet How toRespond to Any Events
That Happen
  • ActionListener interface contains
    actionPerformed(ActionEvent e) method

20
The remove() Method
  • Use to remove components from an applet

21
Example
  • Import javax.swing.
  • Import java.awt.
  • Import java.awt.event.
  • public class JPartyPlanner extends JApplet
    implements ActionListener
  • JLabel companyName new JLabel (Event
    Handlers Incorporated)
  • JButton calcButton new JButton(Calculate
    )
  • JLabel perPersonResult new JLabel(Plan
    with us.)
  • JLabel totalResult new JLabel(The more
    the merrier)
  • Font bigFont new Font(Helvetica,
    Font.ITALIC, 24)
  • public void init( )
  • Container c getContentPane()
  • c.setLayout(new FlowLayout( ) )
  • companyName.setFont(bigFont)
  • c.add(company)
  • c.add(calcButton)
  • c.add(perPersonResult)
  • c. add(totalResult)

22
Example continue
  • public void start ( )
  • perPersonResult.setText(Plan with us.)
  • totalResult.setText(The more the
    merrier)
  • repaint( )
  • public void actionPerformed (ActionEvent e)
  • Object source e.getSource( )
  • if (source calcButton)
  • calculateResult( )
  • public void calculateResult( )
  • String response JOptionPane.showInputDialog(n
    ull, Enter the number of guests)
  • int guestLimit 0, 25, 50, 100, 200,
    500, 1000
  • int ratePerGuest 27, 25, 22, 19, 17,
    14, 11
  • int guests Integer.parseInt(response)
  • int individualFee 0, eventFee 0

23
Example continue
  • int x 0
  • for (x 6 x gt 0 --x)
  • if (guests gt guestLimitx)
  • individualFee ratePerGuestx
  • eventFee guests individualFee
  • x 0
  • perPersonResult.setText( individual per
    person)
  • totalResult.setText(Event total cost
    eventFee)
  • // the end of the calculateResult method
  • // the end of the JPartyPlanner class
Write a Comment
User Comments (0)
About PowerShow.com