Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Java - Applet Hello World 
 

This example introduces you with the Applet in Java. You will learn how to develop applet code and run in the browser.

 

Java - Applet Hello World

                         

This example introduces you with the Applet in Java. You will learn how to develop applet code and run in the browser.

Applet is a program provided by java which is designed for execution within the web browser. Applets are mostly used for a small internet and intranet applications because of small size and it's compatibility among almost all web browsers. Applets are also very secure.  For example, Applets can be used to serve animated graphics on the web.

This example creates a simple applet that displays Hello World message.

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class HelloWorldApplet extends Applet{
  public static void main(String[] args){
    Frame frame = new Frame("Roseindia.net");
    frame.setSize(400,200);
    Applet app = new HelloWorldApplet();
    frame.add(app);
    frame.setVisible(true);
    frame.addWindowListener(new WindowAdapter(){
 
     public void windowClosing(WindowEvent e){
        System.exit(0);
      }
    });
  }
 
 public void paint(Graphics g){
    g.drawString("Hello World!",200,100);
  }
}

Compiling Applets:

javac HelloWorldApplet.java

Running Applet from console:

java HelloWorldApplet

Running Applet from Web browser:

Running applet in browser is very easy job, create an html file with the following code:

<html>
<head>
<title>A Simple Applet program</title>
</head>
<body>
<APPLET CODE="
HelloWorldApplet.class" WIDTH=700 HEIGHT=500>
</APPLET>
</body>
</html> 

The Applet tag in html is used to embed an applet in the web page. 


<APPLET CODE="HelloWorldApplet.class" WIDTH=700 HEIGHT=500>


CODE tag is used to specify the name of Java applet class name. To test your applet open the html file in web browser. You browser should display applet. You can also try this applet online by clicking on the following link.

Try the example online

Download Applet Example

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (
post your own) View All Comments Latest 10 Comments:

This is a very good site.Very informative.This site is very helpful and has solved many problems of mine.

Thanx to the owner.

Posted by Rekha on Friday, 02.22.08 @ 11:58am | #49393

hey, this site is very good for me.. i am solving my problems with this site. I am very thankful to this site owner.

Thanks again.

Mahesh Palghadmal
Web Developer

Posted by Mahesh Palghadmal on Thursday, 01.17.08 @ 11:17am | #45409

import java.awt.*;
import java.awt.event.*;
import java.applet.*;


public class CalculatorApplet extends Applet implements ActionListener
{
private Button keysArray[];
private Panel keyPad;
private TextField lcdField;
private double result;
private boolean first;
private boolean foundKey;
static boolean clearText;
private int prevOperator;

public void init()
{
lcdField = new TextField(20);
keyPad = new Panel ();
keysArray = new Button[17];
result = 0.0;
prevOperator = 0;
first = true;
clearText = true;

setLayout(new BorderLayout());

lcdField.setEditable(false);

for (int i = 0; i <=9; i++)
keysArray[i] = new Button(String.valueOf(i));
keysArray[10] = new Button("/");
keysArray[11] = new Button("*");
keysArray[12] = new Button("-");
keysArray[13] = new Button("+");
keysArray[14] = new Button("=");
keysArray[15] = new Button(".");
keysArray[16] = new Button("CLR");

keyPad.setLayout(new GridLayout (4,4));

for (int i = 7; i <=10; i++)
keyPad.add(keysArray[i]);

for (int i = 4; i <6; i++)
keyPad.add(keysArray[i]);

keyPad.add(keysArray[11]);

for (int i = 1; i <= 3;i++)
keyPad.add(keysArray[i]);

keyPad.add(keysArray[12]);

keyPad.add(keysArray[0]);

for (int i = 15; i >=13; i--)
keyPad.add(keysArray[i]);

add(lcdField, BorderLayout.NORTH);
add(keyPad, BorderLayout.CENTER);
add(keysArray[16], BorderLayout.EAST);

for(int i = 0; i < keysArray.length; i++)
keysArray[i].addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
foundKey = false;

for (int i = 0; i < keysArray.length && !foundKey; i++)
if(e.getSource() == keysArray[i])
{
foundKey = true;
switch(i)
{
case 0: case 1: case 2: case 3: case 4:
case 5: case 6: case 7: case 8: case 9:
case 15:
if (clearText)
{
lcdField.setText("");
clearText = false;
}
lcdField.setText(lcdField.getText() +
keysArray[i].getLabel());
break;

case 10:
case 11:
case 12:
case 13:
case 14:
clearText = true;
if (first)
{
if(lcdField.getText().length()==0)
result = 0.0;
else
result = Double.valueOf(lcdField.getText()).doubleValue();

first = false;
prevOperator = i;
}
else
{
switch(prevOperator)
{
case 10:
result /= Double.valueOf(lcdField.getText()).
doubleValue();
break;
case 11:
result *= Double.valueOf(lcdField.getText()).
doubleValue();
break;
case 12:
result -= Double.valueOf(lcdField.getText()).
doubleValue();
break;
case 13:
result += Double.valueOf(lcdField.getText()).
doubleValue();
break;
}
lcdField.setText(Double.toString(result));
if (i==14)
first = true;
else
prevOperator = i;
}
break;

case 16:
clearText = true;
first = true;
lcdField.setText("");
result = 0.0;
prevOperator = 0;
break;
}
}
}
}

whenever i try to run this or other applets
this msg is shown:

Exception in thread "main" java.lang.NoSuchMethodError: main

although there is no error found at the time of compilation.

Posted by ashok on Monday, 10.29.07 @ 16:15pm | #35063

it is very good site to get knowledge. im very thanksfull to owner of this site and all support for me

Posted by sampat bhawar on Thursday, 06.14.07 @ 15:18pm | #19199

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.