CH13 - Chapter 13 Java: an Introduction to Computer Science...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch Chapter 13 ● HTML ● Applets Applets and HTML Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch Overview ● Applets: Java programs designed to run from a document on the Internet ● HTML: Hypertext Markup Language » a language used to create Internet documents ● Applets run within an HTML document ● HTML documents are run by a web browser, e.g. » Netscape Navigator or » Microsoft Internet Explorer Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch Applets ● Applet: small application ● Applets are Java programs embedded in HTML documents » therefor they run in a browser that views the document ● They are a derived class of the class JApplet , which is derived from Applet. Applet is in turn derived from Panel. » therefor it is both a Container and Component ● JApplet class is in the Swing library so import javax.swing.* Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch Placing JApplet in the Class Hierarchy Class Abstract Class Key: If there is a line between two classes, then the lower class is a derived class of the higher class. AWT Object Component Container Window Panel Frame JApplet Applet Swing ● JApplet is part of Swing and inherits from the Applet class. ● The Applet class is older than JApplet and is not part of AWT or Swing. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch Applet Basics ● When writing applets your list of import statements will usually look like this: import javax.swing.*; import java.awt.*; import java.awt.event.*; ● Applets do not normally have a main method Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch Applet Basics ● Applets do not normally use constructors but they do use a method named init for similar purposes: » setting colors » adding buttons » adding text fields, etc. » the init method has no parameters ● Th HTML document takes care of sizing the applet ● Use a layout manager when defining an applet » use the setLayout method Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch Example: a Trivial Applet HelloApplet.java import javax.swing.*; import java.awt.*; // for Container class public class HelloApplet extends JApplet { public void init(); { Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); JLabel myFirstLabel = new JLabel("Hello out there!"); contentPane.add(myFirstLabel); } } Defining an applet Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch Running an Applet One way to run an applet: » put the applet in an HTML document – good practice is to name the HTML document the same as the applet, but with ".html" instead of ".java" » then run it using appletviewer » like javadoc , javac , and java ,...
View Full Document

This note was uploaded on 04/17/2008 for the course COMP SCI 201 taught by Professor Durga during the Spring '08 term at Wentworth Institute of Technology.

Page1 / 28

CH13 - Chapter 13 Java: an Introduction to Computer Science...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online