Create Applet in Java

Today in Java, i will teach you how to create an applet in Java. An applet is a small application that is viewed on the Web using java. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of sampleApplet.java. In the classname, extend a JApplet to have an applet library. 2. Import javax.swing.* and awt.* package because we will going to have the JLabel in swing and also the Container.
  1. import javax.swing.*;
  2. import java.awt.*;
3. Create a init() function to initialize an array. Then have this code inside the init() function.
  1.  
  2. public void init(){
  3. //create a container that will have the components to be placed
  4. Container con = getContentPane();
  5. // have the flowlayout as the layout manager to directly arrange the component
  6. con.setLayout(new FlowLayout());
  7. // have the label to have the text "Hello World"
  8. JLabel lbl = new JLabel("Hello World!");
  9. // then add the label into the component
  10. con.add(lbl);
  11. }
Note: You have to build your program to have the .class file extension for viewing of the applet. 4. Now, create a new file. Click File Type, choose Other Folder and click HTML Applet. html Applet After creating it, it will have the given code. Then in the Applet Code, put and encode your file name of the created java file, the sampleApplet.class. Follow this code below:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2.  
  3. <html>
  4. <head>
  5. </head>
  6. <body bgcolor="000000">
  7. <center>
  8. <applet
  9. code = "sampleApplet.class"
  10. width = "500"
  11. height = "300"
  12. >
  13. </applet>
  14. </center>
  15. </body>
  16. </html>
Press F5 to run the program. Output: output Full source code of the sampleApplet.java:
  1. import javax.swing.*;
  2. import java.awt.*;
  3.  
  4. public class sampleApplet extends JApplet{
  5.  
  6. public void init(){
  7.  
  8. Container con = getContentPane();
  9. con.setLayout(new FlowLayout());
  10. JLabel lbl = new JLabel("Hello World!");
  11. con.add(lbl);
  12. }
  13.  
  14. }
Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
7 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.