{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lec11 - COMP201 Java Programming Part II GUI Programming...

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

View Full Document Right Arrow Icon
COMP201 Java Programming Part II: GUI Programming Topic 11: Applets Chapter 10
Background image of page 1

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

View Full Document Right Arrow Icon
COMP201 Topic 11 / Slide 2 Outline Introduction What are applets? How to run? Applets basics Class loader and JAR files Security basics Applet life cycle Converting applications to applets Resources for applets Communication with browser
Background image of page 2
COMP201 Topic 11 / Slide 3 Introduction So far, we’ve only covered topics related to stand-alone applications An applet is designed to run within a browser Bring web pages to life Reason behind hype in Java Note: Java is not a language for designing web pages. It is a tool for bring them to life.
Background image of page 3

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

View Full Document Right Arrow Icon
COMP201 Topic 11 / Slide 4 Introduction / JApplet Class An applet is a Java class which extends java.applet.Applet If Swing components are used, the applet must extend from javax.swing.JApplet We will only discuss applets that extend JApplet JApplet is a sub-class of Applet, which in turn is a subclass of Panel Event handling exactly the same as before
Background image of page 4
COMP201 Topic 11 / Slide 5 Introduction /First Applet public class NotHelloWorldApplet extends JApplet { public void init() { Container contentPane = getContentPane(); JLabel label = new JLabel("Not a Hello, World applet", SwingConstants.CENTER); contentPane.add(label); } } // NotHelloWorldApplet.java
Background image of page 5

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

View Full Document Right Arrow Icon
COMP201 Topic 11 / Slide 6 Compare with class NotHelloWorldFrame extends JFrame { public NotHelloWorldFrame() { setTitle("NotHelloWorld"); setSize(300, 200); Container contentPane = getContentPane(); contentPane.add( new NotHelloWorldPanel()); } } public class NotHelloWorld { public static void main ( String [] args) { NotHelloWorldFrame frame = new NotHelloWorldFrame (); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } } Introduction /First Applet
Background image of page 6
COMP201 Topic 11 / Slide 7 Applets are created, run, and destroyed by web browser Don’t set size for an applet: determined by HTML file. Don’t set title for an applet: applets cannot have title bars. No need to explicitly construct an applet. Construction code placed inside the init method. There is no main method. An applet cannot be closed. It terminates automatically when the browser exit. No need to call method show . An applet is displayed automatically. Introduction /First Applet
Background image of page 7

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

View Full Document Right Arrow Icon
COMP201 Topic 11 / Slide 8 Introduction / Running An Applet 1. Compile the .java file 2. Create a HTML file that tells the browser which file to load and how to size the applet <html><body> This is an example. <applet code=“NotHelloWorldApplet.class” width=300 height=300> This text shown if browser doesn’t do Java. </applet> </body></html> 1. View the HTML file with a browser or the command appletviewer (for testing). This works for applets extending Applet , but doesn’t work for those that extend JApplet
Background image of page 8
COMP201 Topic 11 / Slide 9 Introduction / Running An Applet Browser retrieves class file and automatically runs it using its JVM.
Background image of page 9

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

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}