HTML and Applet Basics - PowerPoint PPT Presentation

1 / 31
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:70
Avg rating:3.0/5.0
Slides: 32
Provided by: debbie155
Category:

less

Transcript and Presenter's Notes

Title: HTML and Applet Basics


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

Applets
2
What kind of applications can we program using
Java now?
  • Allowing user inputs
  • Decision Making
  • Generating outputs to user
  • Creating Classes for other applications
  • What not?
  • Respond to user action (e.g., interactive) (Can
    we have a graphical user interface and the
    system can respond to users actions?)
  • Program runs only on a particular environment.
    (Can we run Java in Web environment?)

3
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
4
3 Steps to Create an Applet
  • Create JAVA Program
  • Create .java file
  • Create .class file
  • Design an HTML document that calls your compiled
    .class file
  • Open HTML document (AppletViewer, Netscape
    Navigator, Internet Explorer)

5
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
6
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
7
JJGreet.html
ltHTMLgt ltAPPLET CODE JJGreet.class" WIDTH 450
HEIGHT 200gt lt/APPLETgt lt/HTMLgt
8
Writing an Applet
  • 1. 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)
9
Writing an Applet
  • 2. 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)
10
Writing an Applet
  • 3. Extend your class to include the Applet class
  • public class Greet extends Applet
  • User applets inherit (build upon) traits of the
    Applet class

11
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

12
Methods Automatically Called by the Browser
  • init()
  • Write when there are initialization tasks to
    perform
  • User-defined methods override the Applet class
    methods

13
The start() method
  • Executes after init()
  • Executes every time the applet becomes active
  • Write when there are actions to take when a user
    revisits an applet
  • Example resume animation

14
The stop() method
  • Invoked when a user leaves a Web page
  • Minimize a window
  • Travel to a different Web page
  • Write if action to take when applet is no longer
    visible
  • Usually not necessary to write your own

15
The Applet Life Cycle
16
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)
17
The TextField Component
  • 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)
18
Other TextField Methods
answer.setText(Thank you)
String whatDidTheySay answer.getText()
answer.requestFocus()
answer.setEditable(false)
19
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()
20
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

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

22
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
  • import java.awt.
  • import java.awt.event.
  • import javax.swing.
  • public class AnAppletExample extends Applet
    implement ActionListener

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

Send any ActionEvent messages (button clicks) to
the current applet (GUI)
24
Telling your Applet How toRespond to Any Events
That Happen
  • ActionListener interface contains
  • actionPerformed(ActionEvent e) method
  • public void actionPerformed(ActionEvent e)
  • Object source e.getSource( )
  • if (source pressButton)
  • string name answer.getText()
  • System.out.println(Hi there name )

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

26
Example 1 p.369. Figure 9-13
  • import java.swing.
  • import java.awt.
  • public class JGreet extends JApplet
  • JLabel greeting new JLabel(Hello, who are
    you?)
  • Container con getContentPane() //to
    contain controls
  • FlowLayout flow new FlowLayout() // Ch14
  • con.setLayout(flow)
  • con.add(greeting)

27
Example 2 p.371 Figure 9.16
  • import java.swing.
  • import java.awt.
  • public class JGreet extends JApplet
  • JLabel greeting new JLabel(Hello, who are
    you?)
  • JButton pressMe new JButton(Press Me)
  • JTextField answer new JTextField( , 10)
  • Container con getContentPane()
  • FlowLayout flow new FlowLayout() //Ch14
    for details
  • con.setLayout(flow) //Ch14
  • con.add(greeting)
  • con.add(answer) con.add(pressMe)
  • answer.requestFocus()

28
Example 3 p.375 (Event Response) Figure 9-20
  • import java.swing.
  • import java.awt. import java.awt.event.
  • public class JGreet extends JApplet implements
    ActionListener
  • JLabel greeting new JLabel(Hello, what is
    it?)
  • JButton pressMe new JButton(Press Me)
  • JTextField answer new JTextField( , 10)
  • Container con getContentPane()
  • FlowLayout flow new FlowLayout()
  • con.setLayout(flow)
  • con.add(greeting) con.add(answer)
    con.add(pressMe)
  • answer.requestFocus()
  • aButton.addActionListener(this) //
    responding to users action
  • public void actionPerformed(ActionEvent e)
  • Object source e.getSource()
  • if (source pressme)
  • String name answer.getText()

29
Example 4 Using Applet Methods
  • 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)
  • calcButton.addActionListener(this) //
    event handling add this applet as the
    eventlistener

30
Example 4 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( ) // calling a
    method to do the required function
  • 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

31
Example 4 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