• We just launched and are currently in beta. Join us as we build and grow the community.

Create Applet in Java

sandguy82

Audience Engagement Tactician
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
133
Likes
99
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
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. public

    void

    init(

    )

    {
  2. //create a container that will have the components to be placed
  3. Container

    con =

    getContentPane(

    )

    ;
  4. // have the flowlayout as the layout manager to directly arrange the component
  5. con.setLayout

    (

    new

    FlowLayout

    (

    )

    )

    ;
  6. // have the label to have the text "Hello World"
  7. JLabel

    lbl =

    new

    JLabel

    (

    "Hello World!"

    )

    ;
  8. // then add the label into the component
  9. con.add

    (

    lbl)

    ;
  10. }

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.

appletview.png


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. <

    html>
  3. <

    head>
  4. </

    head>
  5. <

    body bgcolor=

    "000000"

    >
  6. <

    center>
  7. <

    applet
  8. code =

    "sampleApplet.class"
  9. width =

    "500"
  10. height =

    "300"
  11. >
  12. </

    applet>
  13. </

    center>
  14. </

    body>
  15. </

    html>

Press F5 to run the program.

Output:
viewapplet.png


Full source code of the sampleApplet.java:

  1. import

    javax.swing.*

    ;
  2. import

    java.awt.*

    ;

  3. public

    class

    sampleApplet extends

    JApplet

    {

  4. public

    void

    init(

    )

    {

  5. Container

    con =

    getContentPane(

    )

    ;
  6. con.setLayout

    (

    new

    FlowLayout

    (

    )

    )

    ;
  7. JLabel

    lbl =

    new

    JLabel

    (

    "Hello World!"

    )

    ;
  8. con.add

    (

    lbl)

    ;
  9. }

  10. }

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

 

452,496

331,932

331,940

Top