You are on page 1of 6

APPLET IN JAVA

An applet is a small Internet-based program written in Java, a programming


language for the Web, which can be downloaded by any computer. The
applet is also able to run in HTML. The applet is usually embedded in an
HTML page on a Web site and can be executed from within a browser

 An applet is a Java program that runs on a web page

 Applets can be run within any modern browser

 To run modern Java applets, old browsers need an up-to-date


Java plugin

 appletviewer is a program that can run

 An application is a Java program that runs all by itself

To create an applet, you must import the Applet class That class is in
the java.applet package

Applet Class Hierarchy


Programming Applets
Two type of programing in with Applet create.

 Applet Syntax in Java

 Applet Syntax in Html

Applet Syntax in Java

The Applet used in java used the class JApplet and using regular Swing GUI
defined in Classes of Jframe.In That classes no constructors used in the program.

* Now the applet will have a GUI


These are elements to interact with the user.
In this example they will not perform any actions.
We will use no Layout manager;
*/
import java.awt.*;
import java.applet.*;

public class GuiExample extends Applet 


{
 // A Button to click
     Button okButton;
 // A textField to get text input
     TextField nameField;
 // A group of radio buttons
 // necessary to only allow one radio button to be selected at the same time.
     CheckboxGroup radioGroup;
 // The radio buttons to be selected
     Checkbox radio1;
     Checkbox radio2;
 // An independant selection box
     Checkbox option;
 

     public void init()


     {
  // Tell the applet not to use a layout manager.
        setLayout(null);
  // initialze the button and give it a text.
        okButton = new Button("A button");
  // text and length of the field
        nameField = new TextField("A TextField",100);
  // initialize the radio buttons group
          radioGroup = new CheckboxGroup();
  // first radio button. Gives the label text, tells to which
  // group it belongs and sets the default state (unselected)
          radio1 = new Checkbox("Radio1", radioGroup,false);
  // same but selected 
          radio2 = new Checkbox("Radio2", radioGroup,true);
  // Label and state of the checkbox
          option = new Checkbox("Option",false);

  // now we will specify the positions of the GUI components.


  // this is done by specifying the x and y coordinate and
  //the width and height.
          okButton.setBounds(20,20,100,30);
          nameField.setBounds(20,70,100,40);
          radio1.setBounds(20,120,100,30);
          radio2.setBounds(140,120,100,30);
          option.setBounds(20,170,100,30);

  // now that all is set we can add these components to the applet
      add(okButton);
      add(nameField);
      add(radio1);
      add(radio2);
      add(option);
     }

    }

// Thats's it, you now have given the user visual options.
// However there are no actions related to these comonents.
// You will see that later, first an example of a layoutmanager
// that makes it easier to place components.
// Go to LayoutExample.

Make Class For Applet

Run the program .java in the cmd and make the .class file
Applet Syntax in HTML

The applet tag used in the body of the html that embed the java program in the
html<applet>.

The path of class and html page saved same directory

<html>

<head>

</head>

<body>

<applet code=GuiExample.class width=200, height=500>

</body>

</html>

Running The Applet

The two ways to run the java Applets

 Applet viewer

 Html

Applet viewer

The applet viewer where the applet program run on the window. In which
the html file run on the applet viewer and Applet viewer open a new window
and run the class of java

Syntax

C:\Applet viewer file.html


Run With Html

The above code of html run in the browser the output will be

You might also like