Java applet tutorial for beginners
- Details
- Written by Nam Ha Minh
- Last Updated on 09 August 2019   |   Print Email
If you are new to Java applet, this tutorial let you quickly get started with a simple applet from writing code and packaging jar file to embedding in HTML page and running in a browser.
1. Code a Java applet
A Java applet class must extends from java.applet.Appletclass or javax.swing.JApplet class and overrides applet’s life cycle methods: init(),start(), stop() and destroy(). Following is code of a simple Java applet that displays only a button “Click me!”. On clicking the button, a message dialog says “Hello! I am an applet!” appears.
package net.codejava.applet; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JOptionPane; public class QuickApplet extends JApplet { private JButton button; public void init() { button = new JButton("Click me!"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { JOptionPane.showMessageDialog(QuickApplet.this, "Hello! I am an applet!"); } }); System.out.println("APPLET IS INITIALIZED"); } public void start() { getContentPane().add(button); System.out.println("APPLET IS STARTED"); } public void stop() { System.out.println("APPLET IS STOPPED"); } public void destroy() { System.out.println("APPLET IS DESTROYED"); } }
Place the QuickApplet.java file under the directory structure: src\net\codejava\applet
2. Compile and package Java applet
Create a directory called build in the same directory as the src directory. Suppose the current working directory is the parent of the directories build and src, type the following command to compile the applet:
javac -d build src\net\codejava\applet\QuickApplet.java
The compiled .class files will be placed under the build directory. Type the following command to package the applet (not that there is a period at the end):
jar cvf QuickApplet.jar -C build .
That will add all classes in the build directory to a single jar file named QuickApplet.jar.
3. Embed Java applet in HTML page
Now create an HTML file called QuickApplet.html with the following code:
<html> <head> <title>Quick Start Java Applet</title> </head> <body> <center> <applet code="net.codejava.applet.QuickApplet.class" archive="QuickApplet.jar" width="125" height="125" > </applet> </center> </body> </html>
We use to embed the applet in the HTML page. See the article How to show Java applet in HTML page for more details about using the <applet> tag.
4. Run Java applet in browser
Open the QuickApplet.html file in a Java Plug-in enabled browser:
Click on the button, a message dialog appears:
Note that a dialog message created by Java applet has an exclamation mark in the upper right corner.
And following is logging information from Java applet console:
Download the quick applet project in the attachment section.
Other Java Applet Tutorials:
- How to show Java applet in HTML page
- How to sign Java applet
- How to call Javascript function from Java applet
- How to call Java applet's methods and variables from Javascript
- How to resize Java applet to fit browser's window
- How to submitt HTML form in Java applet
- LiveConnect - The API for communication between Java applet and Javascript
Comments
thank you for this article
i built my first java applet
thanks a lot
datadev from paris