java - 无法让我的 java applet 游戏运行 NoClassDefFoundError

标签 java netbeans applet

我正在尝试让小程序运行,但它拒绝运行。这是该小程序的代码。

<html>
  <head>
      <title>GAME!</title>
  </head>
  <body>
      <h1>GAME BY SID</h1>
      <hr>
      <applet code=GameManager.class width=240 height=300>
    alt="Your browser understands the &lt;APPLET&gt; tag but isn't running the applet, for some reason."
    Your browser is completely ignoring the &lt;APPLET&gt; tag!
      </applet>
      <hr>
  </body>
</html>

这是 GameManager 的代码

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package alienLanding;

import java.awt.*;
import java.applet.*;
import java.net.MalformedURLException;
import java.net.URL;
/**
 *
 * @author Admin
 */
public class GameManager extends Applet implements Runnable{
    private int width;
    private int height;
    private Graphics offscreen;
    private Thread animation;
    private Image image;

    private GunManager gm;
    private UFOManager um;

    private Image gunImage;
    private Image ufoImages[];
    private Image[] attackImages;
    private Image[] explodeImages;

    private AudioClip explosionSound;

    private static final int REFRESH_RATE=80;
    @Override
    public void init(){
        showStatus(">>Initializing<<");

        width=bounds().width;
        height=bounds().height;
        setBackground(Color.black);        
        image=createImage(width,height);
        offscreen=image.getGraphics();
        loadImages();

        um=new UFOManager(width,height,ufoImages,attackImages,explodeImages,this,explosionSound);
        gm=new GunManager(width,height,gunImage,this,um.getUFOs());
        um.intialize(gm);
    }
    private void loadImages(){
        MediaTracker t=new MediaTracker(this);
        try{
            URL baseURL;
            URL gunURL;
            //G:\EBOOKS\Java Game Programming\CD\BOOK\chap06
            baseURL=new URL("file:///C:/ShortenedURLProgramming/ch06/");
            gunURL=new URL(baseURL,"image/gun.gif");

            gunImage=getImage(gunURL);
            t.addImage(gunImage,0);

            ufoImages=new Image[6];
            URL UFOImageURL;
            for(int i=0;i<ufoImages.length;++i){              
                UFOImageURL=new URL(baseURL,"image/ufo"+i+".gif");
                ufoImages[i]=getImage(UFOImageURL);
                t.addImage(ufoImages[i],0);
            }

            attackImages=new Image[6];
            URL attackImageURL;
            for(int i=0;i<attackImages.length;++i){
                attackImageURL=new URL(baseURL,"image/attack"+i+".gif");
                attackImages[i]=getImage(attackImageURL);
                t.addImage(attackImages[i],0);
            }

            explodeImages=new Image[4];
            URL explodeImageURL;
            for(int i=0;i<explodeImages.length;++i){
                explodeImageURL=new URL(baseURL,"image/explode"+i+".gif");
                explodeImages[i]=getImage(explodeImageURL);
                t.addImage(explodeImages[i],0);
            }

            URL explosionSoundURL=new URL(baseURL,"Explosion.au");
            try{
                explosionSound=getAudioClip(explosionSoundURL);
            }catch(Exception e){
                System.err.println("Could not load sounds.");
            }
        }catch(MalformedURLException e){
            System.err.println("Incorrect URL. Could not load images, and / or explosion sound.");
            System.exit(1);
        }

        try{
            t.waitForAll();
        }catch(InterruptedException e){

        }

        if(t.isErrorAny()){
            System.err.println(">>Media tracker error.<<");
           // System.exit(1);
        }else if(t.checkAll()){
           showStatus(">>Successfully loaded images.<<");
        }
    }
    @Override
    public boolean mouseMove(Event e,int x,int y){
        gm.moveGun(x);
        return true;
    }
    @Override
    public boolean mouseDrag(Event e,int x,int y){
        gm.moveGun(x);
        return true;
    }
    @Override
    public boolean mouseUp(Event e,int x,int y){
        gm.fireMissile(x);
        return true;
    }
    @Override
    public void update(Graphics g){
        paint(g);
    }
    private void updateManagers(){
        gm.update();
        um.update();
    }
    @Override
    public void paint(Graphics g){
        offscreen.setColor(Color.black);
        offscreen.fillRect(0,0,width,height);
        gm.paint(offscreen);
        um.paint(offscreen);

        g.drawImage(image,0,0,this);
    }
    @Override
    public void start(){
        showStatus(">>Game starting<<");
        animation=new Thread(this);
        if(animation!=null){
            animation.start();
        }
    }
    @Override
    public void stop(){
        showStatus("Game stopped");
        if(animation!=null){
            animation.stop();
            animation=null;
        }
    }
    @Override
    public void run(){
        while(true){
            repaint();
            updateManagers();
            Thread.currentThread().yield();
            try{
                Thread.sleep(REFRESH_RATE);
            }catch(InterruptedException e){}
        }
    }
}

我已将小程序代码粘贴到包含 GameManager.class 的目录中,但没有效果。我正在使用 netbeans。

我很乐意提供更多信息。

请帮助我让它发挥作用。

如果我在 netbeans 中给出“运行文件”,它就可以工作。

最佳答案

改变这个

code=GameManager.class

code=alienLanding.GameManager.class

并确保类文件位于名为 alienLanding 的子文件夹中,以与包匹配。

关于java - 无法让我的 java applet 游戏运行 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25974315/

相关文章:

使用 CPU 和 GPU 添加数组的 Java 基准测试并比较性能

java - 将对象传递给实现 runnable 的类的目的

java - 了解 Java 小程序

java - Volley 没有收到 http 响应,但 postman 收到了

java - 在多模块 Maven 项目中更新模块版本的最佳策略是什么

java - NetBeans "inline variable"重构

java - Netbeans 的 Java 代码中的 while 循环

git - 如何禁用netbeans 7.0 的自动.gitignore 修改?

flash - 包含多个 IP 摄像机流的网页

java - 如何从代码启动java小程序?