chapter 14 applets
Post on 07-Jan-2016
35 Views
Preview:
DESCRIPTION
TRANSCRIPT
Chapter 14
Applets
2
Knowledge Goals
• Understand the differing roles of applications and applets
• Understand how a browser operates• Understand the role of HTML
3
Skill Goals
• Write an applet to perform a simple task• Embed Bytecode within a web page• Construct a simple HTML web page that
executes an applet
4
What Is an Applet?
Applet
A mini-application Distributed along with web pages Run under a browser at the viewer’s site Run under an applet viewer Is distributed in Bytecode form
5
What Is an Applet?
Applets differ from applications in several ways Applets don’t have a main method Applets are invoked differently Applets are subject to more security
constraints Applets are not in control of their own
destiny Applets do not have constructors;
initializations are done in method init
6
What Is an Applet?
Inheritancehierarchy
forAWT and
Swing
7
How Do We Write Applets?
// Applet Factorial computes the factorial of
// its input and stores it in a variable of
// type int, which is displayed on the
// screen.
import javax.swing; // JApplet class
import java.awt.*; // User interface classes
import java.awt.event.*; // Event classes
public class FactInt extends Applet implements ActionListener {…}
8
public void actionPerformed(ActionEvent event)
{
int value;
value =
Integer.parseInt(inputField.getText());
inputField.setText("");
outLabel.setText(value + " factorial is ”
+ factorial(value));
}
How Do We Write Applets?
Method call
9
How Do We Write Applets?
private int factorial(int n)
// Assumption: n is not negative.
{
if (n == 0)
return 1;
else return (n * factorial((n-1)));
}
Now, we have to set up a button, a label, and a text input field
10
How Do We Write Applets?
// Setting up a button, label, and input
// field
private static JTextField inputField;
private static JLabel label;
private static JLabel outLabel;
private static JButton button;
We need to write method init
11
How Do We Write Applets?
public void init(){ // Instantiate components label = new JLabel("Enter an integer; ” + “press Enter."); outLabel = new JLabel("Answer"); button = new JButton("Enter"); button.addActionListener(this); inputField = new JTextField("Value here");
Note
12
// Add components
add(label);
add(inputField);
add(button);
add(outLabel);
// Specify a layout manager for the
// window object
setLayout(new GridLayout(4,1));
}
How Do We Write Applets?
Why are the add s not applied to an object?
13
How Do You Run an Applet?
The Bytecode version of an applet is run on the user’s browser
Yes, but how?
Some definitions are in order first
Computer network A collection of computing devices connected in order to communicate and share resources
Can you name some of the devices in a computer network?
14
How Do You Run an Applet?
Local area network (LAN)
A network that connects a relatively small number of machines in a relatively close geographical area
Wide area network (WAN)
A network that connects local area networks over a potentially large geographic distance
Internet
A wide area network that spans the planet
15
How Do You Run an Applet?
The Web An infrastructure of information combined and the network software used to access itUniform Resource Locator (URL)A standard way of specifying the location of a Web page, containing the hostname, "/", and a file
What is the relationship between theInternet and the Web?
16
The World Wide Web
Why is the expression"visiting a website"
confusing?
17
How Do You Run an Applet?
Hypertext Markup Language (HTML)The language used to create or build a Web pageMarkup language A language that uses tags to annotate the information in a documentTagsThe syntactic element in a markup language that indicates how information should be displayed
18
How Do You Run an Applet?
HTML
19
How Do You Run an Applet?
20
How Do You Run an Applet?
21
How Do You Run an Applet?
HTML Tags are enclosed in angle brackets
(<. . . >) Words such as HEAD, TITLE, and
BODY are called elements and specify the type of the tag
Tags are often used in pairs, with a start tag such as <BODY> and a corresponding end tag with a / before the element name, such as </BODY>
22
How Do You Run an Applet?
The browser determines how the page should be displayed based on the tagsThe browser
Ignores the way we format the HTML document using carriage returns, extra spaces, and blank lines
Takes into account the width and height of the browser window
Reformats the contents to fit your browser window
23
How Do You Run an Applet?
Tags
<HTML>…</HTML>
<HEAD>…</HEAD>
<TITLE>…</TITLE>
<BODY>…</BODY>
<P>…</P>
<HR>
<B>…</B>
<I>…</I>
<PRE>…</PRE>
Meaning
Enclose document
Enclose heading
Enclose title
Enclose body of document
Enclose paragraph
Insert horizontal rule
Enclosed should be boldface
Enclosed should be in italics
Preformatted; display text
as-is
24
How Do You Run an Applet?
<APPLET code = “fileName.class” width=250height=150></APPLET>
<APPLET code = “FactInt.class” width=250height=150></APPLET>
Note FactInt.class is the file with the Bytecode version of the Applet FactInt
25
Yes, But How Do You Run an Applet?
If you are in an integrated environment, your Java system will run the applet in the viewer
To run in a browser, compile the applet name the Bytecode file with a .class extension create the web page with the appropriate
<applet …> put the .class file in the same directory as the web
page access the web page
26
How Do You Run an Applet?
Go tothiswebpageand
followthe
directions
Be sure FactInt.class is with this page
27
How Do You Run an Applet?
28
How Do You Run an Applet?
top related