APPLETS Write an HTML documents to host an applet Understand simple - - PowerPoint PPT Presentation

applets
SMART_READER_LITE
LIVE PREVIEW

APPLETS Write an HTML documents to host an applet Understand simple - - PowerPoint PPT Presentation

APPLETS Write an HTML documents to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple Swing applet and use a JLabel Add JTextField and JButton components to Swing applet Using the


slide-1
SLIDE 1

APPLETS

  • Write an HTML documents to host an applet
  • Understand simple applets
  • Use Labels with simple AWT applets
  • Write a simple Swing applet and use a JLabel
  • Add JTextField and JButton components to Swing applet
  • Using the setLocation() and setEnabled() methods

1

slide-2
SLIDE 2

Write an HTML documents to host an Applet (1)

  • As you know, applications are stand-alone programs.
  • In contrast, applets are programs that are called

from within another application.

  • You run applets within a page on the internet, an

intranet, or a local computer from within another program called Applet Viewer, which comes with the Java Developer’s kit.

2

slide-3
SLIDE 3

Write an HTML documents to host an Applet (2)

  • To view an applet, it must be called from within

another document written in HTML – Hypertext Markup Language.

  • HTML, is a simple language used to create Web

pages for the Internet.

  • HTML, contain many commands that allow you to

format text on a Web pages, import graphic images and link your page to other Web pages.

3

slide-4
SLIDE 4

When you create an applet, you do the following:

  • Write the applet in the java programming language,

and save it with a .java file extension, just as when you write a java application.

  • Compile the applet into bytecode using the javac

command, just as when you write a java application.

  • Write an HTML document that includes a statement

to call your compiled java class.

4

slide-5
SLIDE 5

HTML

  • We don’t need to learn the entire HTML

language;

  • We need to know two of the HTML

commands, called tags.

  • The tag that begins every HTML document is

<html> - is not case sensitive.

  • The tag that end every HTML document is

</html>

5

slide-6
SLIDE 6

To run an Applet from within an HTML document

  • You add an <APPLET> and </APPLET> tag pair.
  • Usually you place three attributes within the <APPLET> tag:
  • CODE, WIDTH and HEIGHT, attributes or arguments – with

them HTML tag can do something in a certain way.

EXAMPLE:

  • <APPLET CODE = “Aclass.class” WIDTH = 300 HEIGHT = 200>
  • </APPLET>

6

slide-7
SLIDE 7

Understanding Simple Applets

Structure of Applet and JApplet classes:

  • Java.lang.Object
  • Java.awt.Component
  • Java.awt.Container
  • Java.awt.Panel
  • Java.applet.Applet
  • Javax.swing.JApplet

7

slide-8
SLIDE 8

Four Methods included in every Applet(1)

  • In an application, the main() method calls other

methods (the following are included in every applet) you write:

  • public void init()
  • Called once by the applet container when the applet

is loaded for execution.

  • action performed here are initializing fields, creating

GUI components, loading sounds to play, loading images to display.

8

slide-9
SLIDE 9

Four Methods included in every Applet(2)

  • public void start()
  • called by the applet container after method

init completes execution.

  • in addition, if the user browses to another

website and later returns to the applet’s HTML page, method start is called again.

9

slide-10
SLIDE 10

Four Methods included in every Applet(3)

  • public void paint( Graphics g )
  • Called by the applet container after methods

init and start.

  • method paint is also called when the applet

needs to be repainted.

10

slide-11
SLIDE 11

Four Methods included in every Applet(4)

  • public void stop()
  • This method is called by the applet container when

the user leaves the applet’s web page by browsing to another web page.

  • public void destroy()
  • this method is called by the applet container when

the applet is being removed from memory.

11

slide-12
SLIDE 12

Using Labels with Simple AWT Applet

  • Label is a built-in class that holds text that you can

display within an applet.

  • Calling a label constructor without any arguments,

example: Label greetings = new Label

  • You can assign some text to the label with the:

setText() method

  • Example:

greetings.setText(“Hi There”); or greetings = new Label(“Hello. Who are You”);

12

slide-13
SLIDE 13
  • To add a component to an applet window, you

use the add() method.

  • Example: if a Label is defined as

Label greeting = new Label(“Hello. Who are You”);

  • You can place a greeting within an applet using

the command: add(greeting);

13

slide-14
SLIDE 14

To create and run the Greet Applet

  • Open a new text file in your text editor.
  • Enter the code shown on the next slide.

14

slide-15
SLIDE 15

Program to create an applet that displays “Hello. Who are You” import java.applet.*; import java.awt.*; public class Greet extends Applet { Label greeting = new Label(“Hello. Who are You”); public void init() { add(greeting); } }

15

  • Save the file as Greet.java.
  • Compile the program.
slide-16
SLIDE 16

To create a simple HTML document to run Greet

  • Open a new file in your text editor.
  • Type the opening HTML tag, <HTML>.
  • On the next line, type the opening APPLET tag that contains

the applet’s name and dimensions: <APPLET CODE = “Greet.class” WIDTH = 450 HEIGHT = 200>

  • On the next line, type the applet’s closing tag:</APPLET>
  • On the next line, type the closing HTML tag: </HTML>
  • Save the file as TestGreet.html

16

Open a new file in your text editor, and then type the HTML document that run the Applet.

slide-17
SLIDE 17

Write a simple Swing Applet and using a JLabel

  • The counterpart to the AWT Label is a JLabel.
  • JLabel is a built-in class that holds text that

you can display within an applet.

17

slide-18
SLIDE 18

Structure of JLabel class

  • Available constructors for the JLabel class.
  • JLabel() creates a JLabel instance with no

image and with an empty string for the title.

  • JLabel(icon image) creates a JLabel instance

with the specified image.

18

slide-19
SLIDE 19

Content Pane

  • It is an object of the container class from the

Java.awt package.

  • A container object can be created using the

getContentPane() method.

  • To create a container object named con, the syntax

to use is: Container con = getContentPane();

  • Then the stmt: JLabel greeting = new JLabel();
  • Adds the greeting object to the content pane with

the stmt: con.add(greeting);

19

slide-20
SLIDE 20

Creating JGreet

  • Open a new text file in your text editor.
  • Enter the code on the next slide.
  • Save the file as JGreet.java

20

slide-21
SLIDE 21

Creating JGreet

import javax.swing.*; import java.awt.*; public class JGreet extends JApplet { JLabel greeting = new JLabel("Hello. Who are you?"); public void init() { Container con = getContentPane(); con.add(greeting); } }

21

slide-22
SLIDE 22

Creating JGreet

  • Compile the program.
  • Open a new file in your text editor, and then

type the html document that will run the JApplet.

  • Save the file as TestJGreet.html.

22

slide-23
SLIDE 23

Changing a JLabel’s Font

  • You use: setFont() method.
  • To construct a font object, you need three

arguments: typeface, style, and point size.

  • The typeface is a string representing a font.

23

slide-24
SLIDE 24

Changing a JLabel’s Font

  • To create font object, use the synthax:

Font headlineFont = new Font(“Tahoma”, Font.BOLD, 36)

  • Then you use the setFont method to assign

the font to a Label with the stmt: greeting.setFont(headlineFont);

24

slide-25
SLIDE 25

To change the appearance of the greeting in the JGreet Applet

  • Open the Jgreet.java file in your text editor and change the class name to

JGreet2.

  • Position the insertion point at the end of the line that declares the

greeting Label, and then press enter to start a new line of text.

  • Declare a Font object named bigFont by typing the following:

Font bigFont = new Font(“Arial Black”, Font.ITALIC, 24);

  • Place the insertion point to the right of the opening curly brace of the

init() method, and then press Enter to start a new line.

  • Set the greeting font to bigFont by typing greeting.setFont(bigFont);
  • Save the file using the filename JGreet2.java.
  • Compile the program
  • Run the applet, changing the TestJGreet.html document to

TestJGreet2.html.

25

slide-26
SLIDE 26

Adding JTextField and JButton Components to Swing Applets

  • You can construct a JTextField object using one of

several constructor:

  • Public JTextField() :constructs a new JTextField.
  • Public JTextField(int numcolumns) :constructs a new empty

JTextField with a specified number of columns.

  • Public JTextField(String text) :constructs a new JTextField

initialized with the specified text.

  • Public JTextField(String text int columns) :constructs a new

JTextField initialized with the specified text and columns.

26

slide-27
SLIDE 27
  • To provide a JTextField for a user to answer the “Who are

you?” question, you can code: JTextField answer = new TextField(10)

  • To add the JTextField named answer to an applet, you write

con.add(answer); where con is container object declared as Container con = getContentPane();

  • The setText() method; allows you to change the text in a

JTextField that has already been created as in answer.setText(“Thank you”);

  • The getText() method, allows you to retrieve the string of text

in a JTextField

  • requestFocus() method, allows the insertion point to appears

within the JTextField.

27

slide-28
SLIDE 28

Jbutton Constructors

  • Public Jbutton() creates a button with no set

text.

  • Public Jbutton(Icon, icon) creates a button

with an icon of type Icon or ImageIcon.

  • Public Jbutton(String text) creates a button

with text.

  • Public Jbutton(String text, Icon icon) creates a

button with initial text and an icon of type Icon or ImageIcon.

28

slide-29
SLIDE 29

To change the appearance of the greeting in the JGreet applet

  • Open the JGreet2.java file in your text editor and change the class name to

JGreet3.

  • Position the insertion point at the end of the line that declares the

greeting Label, and then the press enter to start a new line of text.

  • Declare a Jbutton with the label “Press Me” and an empty JTextField by

typing following: JButton pressMe = new Jbutton(“Press Me”); JTextField answer = new JTextField(“ ”,10);

  • Set the new layout manager to a flow layout with the stmt: FlowLayout

flow = new FlowLayout();

  • Position

the insertion point at the end

  • f

the statement con.add(greeting); and press Enter to start a new line and type con.setLayout(flow).

29

slide-30
SLIDE 30
  • Add the JTextField and the Jbutton to the Swing applet by

typing the following: con.add(answer); con.add(PressMe);

  • On the next line, request focus for the answer by typing:

answer.requestFocus();

  • Save the file using the filename JGreet3.java.
  • Compile the program
  • Run the applet, changing the TestJGreet.html document to

TestJGreet3.html.

30

slide-31
SLIDE 31

Preparing your Swing Applet to Accept Event Messages

  • Telling your Swing Applet to Expect Events to

Happen.

  • addActionListen() method
  • aButton.addActionListen(this);
  • “this reference” means “this current method”

31

slide-32
SLIDE 32
  • Telling your Swing Applet How to Respond to any Events that

Happen:

  • The ActionListen interface contains the:

actionPerformed(ActionEvent e) method specification.

  • You use the header:

public void actionPerformed(ActionEvent e), where “e” is any name you choose for the Event (the JButton click) that initiate the notification of the ActionListener(the JApplet).

32

slide-33
SLIDE 33

To add functionality to your Swing Applet

  • Open the JGreet3 file in your text editor and change the class name

to JGreet4.

  • Add a third import stmt to your program by typing:

import java.awt.event.*;

  • Position the insertion point at the end of the class header: public

class JGreet4 extends JApplet, press spacebar and then type: implements ActionListener

  • Position the insertion point at the end of the stmt in the init()

method that adds the pressMe button to the JApplet, and press

  • enter. Prepare your Swing applet for Jbutton-sourced events by

typing the stmt: pressMe.addActionListener(this);

33

slide-34
SLIDE 34
  • Position the insertion point to the right of the closing curly brace for the

init() method, and then press Enter. Add the following actionPerformed() method which follows the init() method but comes before the closing brace for the JGreet4 class. Use the object’s getSource() method to determine that the source of the event is the JButton. Use an if stmt to control the events that occur when the event’s source is the JButton. You will declare a String to hold the user’s name, use the getText() method on the answer JTextField to retrieve the String, and display an on-screen message to the user. public void actionPerformed(ActionEvent thisEvent) { Object source = thisEvent.getSource(); if (source == pressMe) { String name = answer.getText(); System.out.println(“Hi Mr\Mrs” + name); } }

34

slide-35
SLIDE 35
  • Save the file as JGreet4.java and compile the program.

35

slide-36
SLIDE 36

To add the ability to press [Enter] from within the JTextField for input

  • In the JGreet4.java text file, change the class name to JGreet5, position

the insertion point at the end of the stmt: pressMe.addActionListener(this);

  • Make the answer field accept input by typing:

answer.addActionListener(this);

  • Add the following stmts to the end of the ActionPerformed method that

uses the instanceof keyword to test for an action event generated by JTextField named answer: else if (source instanceof JTextField) { String name = answer.getText(); System.out.println(“Hi Mr/Mrs” + name) }

36

slide-37
SLIDE 37

Adding Output to a Swing Applet

  • To add a personalGreeting JLabel to the Swing applet:
  • Within the JGreet5.java text file, change the class name to JGreet6, and then

remove both System.out.println(“Hi Mr/Mrs” + name); stmts from the actionPerformed() method.

  • Position the insertion point at the end of the stmt: JTextField answer =

JTextField(“ ”, 10); and press Enter. To declare a new JLabel named personalGreeting, type the stmt: JLabel personalGreeting = new JLabel(“ ”);

  • Position the insertion point in the init() method after the con.add(pressMe);

stmt, and then add the JLabel personalGreeting with the stmt: con.add(personalGreeting);

  • Add the following stmt to the actionPerformed() method after the String

name = answer.getText(); stmt to set the text of the personalGreeting. Be sure to Add the stmt to the body of both the if and if…else stmts: personalGreeting.setText(“Hi Mr/Mrs” + name);

  • Save the program as JGreet6.java

37

slide-38
SLIDE 38

Removing Components from Applet

  • If you can add components to an applet, you should also be

able to remove them; you do so with the synthax: remove() method.

  • Open the JGreet6.java file and rename the class JGreet7.

Place insertion point after the closing curly brace of the if..else stmt in the actionPerformed() method, type these stmts: remove(answer); remove(pressMe); repaint();

  • Save the file as JGreet7.java.

38

slide-39
SLIDE 39

Creating an Interactive Party Planner Swing Applet

  • Open a new text file in your text editor.
  • Type the following import stmts, the JPartyPlanner class

header and the opening curly brace for the class:

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class JPartyPlanner extends JApplet implements ActionListener

39

slide-40
SLIDE 40
  • You will need several components: a JLabel for

company name, a JButton the user can click to perform a calculator, and two more JLabels to display output:

JLabel companyName = new JLabel("Event Handlers Incorporated"); JButton calcButton = new JButton("Calculator"); JLabel perPersonResult = new JLabel("Plan with us."); JLabel totalResult = new JLabel("The more the merrier!");

  • Add a Font Object by typing the following:

Font bigFont = new Font("Helvetica", Font.ITALIC, 28);

40

slide-41
SLIDE 41
  • Use the int() method to place components within

the applet screen, and then prepare the JButton to receive action messages by typing the following:

public void init() { Container con = getContentPane(); con.setLayout(new FlowLayout()); companyName.setFont(bigFont); con.add(companyName); con.add(calcButton); calcButton.addActionListener(this); con.add(perPersonResult); con.add(totalResult); }

41

slide-42
SLIDE 42
  • Add the following start() method, which

executes when the user leaves the Swing Applet and resets the JLabel and the data- entry JTextField:

public void start() { perPersonResult.setText("Plan with us."); totalResult.setText("The more the merrier!"); repaint(); }

42

slide-43
SLIDE 43
  • Next you will create the actionPerformed() method.

You begin by declaring two parallel arrays – one array will hold guest limits for each of six event rates, and two arrays for guest limits and rates:

public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == calcButton) { String response = JOptionPane.showInputDialog(null, "Enter the number of Guest"); int[] guestLimit = {0, 25, 50, 100, 200, 500, 1000}; int[] ratePerGuest = {27, 25, 22, 19, 17, 14, 11};

43

slide-44
SLIDE 44
  • Next add the following variable to hold the number
  • f guests. The user will receive input from a dialog

box, but you need an integer to perform calculations so you can use the parseInt() method: int guests = Integer.parseInt(response);

  • You need two variables – one will hold the

individual, per-person fee for an event, and the

  • ther will hold the fee for the entire event:

int individualFee = 0, eventFee = 0;

44

slide-45
SLIDE 45
  • Enter the following variables to use as

subscripts for the arrays: int x = 0, a = 0;

  • Enter for loop:

for(x = 6; x >= 0; --x) if (guests >= guestLimit[x]) { individualFee = ratePerGuest[x]; eventFee = guests * individualFee; x = 0; }

45

slide-46
SLIDE 46
  • Enter code to produce output for the user:

perPersonResult.setText("$" + individualFee + " Per Person"); totalResult.setText("Event Cost $" + eventFee); } } }

  • Write HTML Document to test the applet with width = 320

and Height = 200.

46

slide-47
SLIDE 47
  • Shortcoming of objects you have written so far

is that you cannot choose the location of the JLabel and JButton objects you place within your Swing Applets.

47

Using the setLocation() & setEnable() Methods

slide-48
SLIDE 48

The setLocation() Method

  • setLocation() method allows you to place a

component at a specific location within the Applet Viewer window.

48

slide-49
SLIDE 49

Example:

  • To position a Label object named someLabel

at the upper-left corner of a window, you write: someLabel.setLocation(0,0);

  • If a window is 200 pixels wide and 100 pixels

tall, then you can place a Button named pressMe in the approximate center of the window with the statement: pressMe.setLocation(100,50);

49

slide-50
SLIDE 50
  • Illustrate the screen coordinate positions.
  • Example:

aButton.setLocation(100,100);

  • Places the JButton outside the window,

where you cannot see the component.

50

slide-51
SLIDE 51

The setEnable() Method

  • This can be used with a component to make it

unavailable and, in turn, to make it available again.

  • The setEnabled() method takes an argument
  • f true if you want to enable a component, or

false if you want to disable a component.

  • Example: pressButton.setEnabled(false);

51