headscratch
headscratch

Reputation: 551

Creating and running a Java applet

I thought I would try my hand at applets - I made an applet using Eclipse. It runs fine using the Run As -> Java Applet.

I read a bit about running it outside Eclipse, so I did the following:

  1. Made a folder.
  2. Created New -> Java Project [applet_test].
  3. Inside the project, I created New -> Other -> Visual Swing Class -> Applet [Number1] - that created Number1.class.
  4. Added code and ran it as a Java applet - it ran fine.
  5. Exported the project as a JAR file (not a runnable JAR file).
  6. Wrote HTML using TextEdit (Mac's version of Windows' Notepad). The HTML follows, below...
  7. I put the JAR file, HTML and .class file in the folder.
  8. In Terminal (Mac's version of Windows command prompt window), I ran Appletviewer applet_testX2.html (that's the name of my HTML).
  9. I could see a brief flash of the application name at the top of the screen (as would any other running application).

However, the application (which should display a Jpanel with a label and a button) did NOT appear. I also tried running it from Firefox and Safari. Only the HTML code appeared.

So, what am I doing wrong? And, more importantly, how do I do it correctly?

Code follows without imports statements:

<html>
    <body>
        <applet code="Number1.class" archive="applet_test.jar"
            width=300
            height=300>
        </applet>
    </body>
</html>

The Java code:

public class Number1 extends JApplet {
    public Number1() {

    }

    private static final long serialVersionUID = 1L;

    @Override
    public void init() {
        try {
            EventQueue.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    initComponents();
                }
            });
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    private void initComponents() {
        setSize(320, 240);

        JPanel panel = new JPanel();
        getContentPane().add(panel, BorderLayout.CENTER);

        JLabel lblAppletTest = new JLabel("Applet test 1");
        panel.add(lblAppletTest);

        JButton btnPushIt = new JButton("Push it");
        panel.add(btnPushIt);
    }
}

Firefox source view:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta http-equiv="Content-Style-Type" content="text/css">
        <title></title>
        <meta name="Author" content="BT">
        <meta name="Generator" content="Cocoa HTML Writer">
        <meta name="CocoaVersion" content="1038.35">
        <style type="text/css">
            p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Helvetica}
            p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Helvetica; min-height: 19.0px}
        </style>
    </head>
    <body>
        <p class="p1">&lt;html&gt;</p>
        <p class="p1"><span class="Apple-converted-space"> </span>&lt;body&gt;</p>
        <p class="p1"><span class="Apple-converted-space">   </span>&lt;applet code="Number1.class" archive="applet_test.jar"</p>
        <p class="p2"><br></p>
        <p class="p1"><span class="Apple-converted-space">    </span>width=300</p>
        <p class="p1"><span class="Apple-converted-space">   </span>height=300&gt;</p>

        <p class="p1">&lt;/applet&gt;</p>
        <p class="p1">&lt;/body&gt;</p>
        <p class="p1">&lt;/html&gt;</p>
    </body>
</html>

Upvotes: 2

Views: 12518

Answers (2)

Ameer Chand
Ameer Chand

Reputation: 51

                 Using Appletviewer
                 ------------------
  1. Write code of Applet.

  2. If you installed tomcat in D:

  3. code

-

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

public class MyApplet extends Applet
{
    public void init()
    {
        System.out.println("init intilize");
        GridLayout g=new GridLayout(4,6,0,0);
        setLayout(g);
        MyListener m=new MyListener();

        for(int i=1;i<=12;i++)
        {
            Button b=new Button("ok"+i);
            add(b);
            b.addActionListener(m);
        } 
    }//end of init

    public void start()
    {
        System.out.println("applet started");
    }//end of start

    public void stop()
    {
        System.out.println("applet stop");
    }//end of 

    public void paint(Graphics g)
    {
      g.drawString("Naveed",200,25);
      g.drawOval(20,30,30,20);
      System.out.println("applet paint");
    }//end of start

    public void destroy()
    {
        System.out.println("applet destroy");
    }//end of start
}

class MyListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        System.out.println("button clicked");
    }//end of actionPerformed
}

Now save this code in D:, not in sub folder.

First Compile it.

open the cmd

cd D:

type

`javac MyApplet.java -d classpath D:\Tomcat\common\lib\servlet.jar`

This will make a MyApplet.class file

Now make a html file.

<html>
<body>
    <applet code="Number1.class" width=30  height=300 > </applet>
</body>
</html>

Save with the name of you want let's say app.html

run the html file now.

In the cmd window

appletviewer app.html

Output will be in front of you.

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285450

My guess is that here:

<applet code="Number1.class" archive="applet_test.jar"

you're not taking packages into consideration. For instance, if the package is myPackage.vol3 then the line should read

<applet code="myPackage.vol3.Number1.class" archive="applet_test.jar"

But if this doesn't help, you'll want to extract any error messages that the browser gives you and edit your original post to show us what they are.

Upvotes: 2

Related Questions