Cs423 Java Applet
Cs423 Java Applet
Objective:
Learn how to develop Java programs that interact with users through a Web browser
CSTP WS00
CS423 (cotter)
start ( )
Called when applet becomes visible (page called up). Called every time applet becomes visible.
stop ( )
Called when applet becomes hidden (page loses focus).
destroy ( )
Guaranteed to be called when browser shuts down.
CSTP WS00 CS423 (cotter) 2
Security
CSTP WS00
CS423 (cotter)
Applet Presentation
Uses applet Tag in HTML
<HTML> <TITLE> MyNewApplet </TITLE> <BODY> Heres My first Applet <APPLET CODE = MyNewApplet WIDTH = 300 HEIGHT = 200> </APPLET> </BODY> </HTML>
CSTP WS00
CS423 (cotter)
CSTP WS00
CS423 (cotter)
MyTextApplet
import java.applet.*; import java.awt.*; import java.awt.event.*; public class MyTextApplet extends Applet { public void init() { setBackground(Color.gray); Panel p = new Panel(); ButtonAction aButtonAction = new ButtonAction(); p.setLayout(new FlowLayout()); tickButton = new Button("Tick"); tickButton.addActionListener(aButtonAction); p.add(tickButton);
CSTP WS00 CS423 (cotter) 6
MyTextApplet
setButton = new Button("Set time"); setButton.addActionListener(aButtonAction); p.add(setButton); hourField = new TextField("12", 3); p.add(hourField); minuteField = new TextField("00", 3); p.add(minuteField); timeField = new TextField("", 12); p.add(timeField); add(p); }
CSTP WS00
CS423 (cotter)
MyTextApplet
Class ButtonAction implements ActionListener { public void actionPerformed(ActionEvent evt) { String buttoncommand = evt.getActionCommand(); if (buttoncommand.equals("Tick")) { int minutes = Integer.parseInt(minuteField.getText()); minutes += 1; String min = String.valueOf(minutes); minuteField.setText(min); }
CSTP WS00
CS423 (cotter)
MyTextApplet
else if (buttoncommand.equals("Set time")) { int hours = Integer.parseInt(hourField.getText()); int minutes = Integer.parseInt(minuteField.getText()); String tim = hourField.getText() + ":" + minuteField.getText(); timeField.setText(tim); } } } //end of ButtonAction private private private private private }
CSTP WS00 CS423 (cotter) 9
TextField hourField; TextField minuteField; TextField timeField; Button tickButton; Button setButton;
CSTP WS00
CS423 (cotter)
10
MyTextApplet Output
CSTP WS00
CS423 (cotter)
11
CSTP WS00
CS423 (cotter)
12
CSTP WS00
CS423 (cotter)
13
FontApplet.htm
CSTP WS00
CS423 (cotter)
14
CSTP WS00
CS423 (cotter)
start ( )
Called when applet becomes visible (page called up). Called every time applet becomes visible.
stop ( )
Called when applet becomes hidden (page loses focus).
destroy ( )
Guaranteed to be called when browser shuts down.
CSTP WS00 CS423 (cotter) 16
LifecycleApplet.java
import java.applet.Applet; import java.awt.Graphics; public class LifecycleApplet extends Applet { public LifecycleApplet() { System.out.println("Constructor running..."); } public void init() { System.out.println("This is init."); } public void start() { System.out.println("Applet started."); } public void paint(Graphics theGraphics) { theGraphics.drawString("Hello, World!", 0, 50); System.out.println("Applet just painted."); } public void stop() { System.out.println("Applet stopped."); } public void destroy() { System.out.println("Applet destroyed."); } CSTP CS423 (cotter) 17 } WS00
LifecycleApplet.java
CSTP WS00
CS423 (cotter)
18
LifecycleApplet.java
CSTP WS00
CS423 (cotter)
19
Summary
Java Applet managed through applet lifecycle Applet can interact with browser through HTML code Uses Event programming model to interact with user (using buttons, textfields, etc.)
CSTP WS00
CS423 (cotter)
20