Вопрос

I'm trying to make an sideshow program using Java applet, and everything is fine when I make and view the JApplet using eclipse but when I run the code in my browser I get NoClassDefFoundError which means there are some parts of my program that are undefined in JApplet.

Here is my code:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;

public class Applet extends JApplet {
    private int i = 0;
    private Button prev = new Button("prev");
    private Button next = new Button("next");

    public void init() {
        setSize(1100, 875);
        prev.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (i == 0)
                    i = getList().length - 1;
                else
                    i--;
                repaint();
            }
        });

        next.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (i == getList().length - 1)
                    i = 0;
                else
                    i++;
                repaint();
            }
        });

        Panel buttons = new Panel();
        buttons.setLayout(new FlowLayout());
        buttons.add(prev);
        buttons.add(next);
        setLayout(new BorderLayout());
        add("South", buttons);
    }

    public void paint(Graphics g) {
        setImg(g, getImgPath());
    }

    public String[] getList() {
        String[] str = { "./ (1).jpg", "./ (2).jpg", "./ (3).jpg","./ (4).jpg","./ (5).jpg","./ (6).jpg","./ (7).jpg","./ (8).jpg" };
        return str;
    }

    public String getImgPath() {
        return getList()[i];
    }

    public void setImg(Graphics g, String str) {
        try {
            URL url = new URL(getCodeBase(), str);
            BufferedImage img = ImageIO.read(url);
            g.drawImage(img, 50, 50, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

My html code

<html>
<meta http-equiv="Content-Type" content="text/html; charset=Cp1252" />

<body>
    <applet code=Applet.class width="200" height="200">
    </applet>
</body>

</html>

Screen shot: enter image description here

Download everything: https://www.dropbox.com/s/v0jqui0nomfb4zx/Code.zip

My question is which part of my program is undefined for JApplet

Thank you.

Это было полезно?

Решение

The symbol $Applet$1 refers to an anonymous inner class, probably your applet's ActionListener. Your HTML needs to specify the location of the JAR containing all your classes, not just the enclosing class. Here are several things to consider:

  • Make your applet a hybrid for ease of development.

  • Use , as shown in the second example, for ease of deployment.

  • Don't use an existing superclass name, e.g. Applet, for your subclass.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top