繁体   English   中英

为什么会出现NoClassDefFoundError?

[英]Why am I getting NoClassDefFoundError?

我正在创建一个基本程序,当按下空格键时,将使“生命线”充满,并且当您达到最大极限时,您将获胜。 它每秒钟减少一次,因此只需向空格键发送垃圾邮件即可获胜。 它正在尝试通过Java测试基本游戏。 我的程序陷入了这个错误,给了我不熟悉的运行时错误。 这是我的代码:

import javax.swing.JOptionPane;
import javax.swing.*;               
import java.util.*;                             
import static java.lang.System.*;
import static java.lang.Math.*;

import java.awt.*;                              
import java.awt.event.*;                                
import java.io.*;   //for files                     
public class clicker
{
    public static void main(String args[])
    {
        new Clicker();  //Make a window
    }

}
class Clicker extends Frame   implements KeyListener, MouseListener
{
    // global variables
    private final static int SCHEIGHT=768,SCWIDTH=1024;
    // direction constants

    final static int N = 0,NE = 1,E = 2,SE = 3, S = 4, SW = 5, W = 6, NW =7,STILL = 8;
    // movement change constants
    final  int X = 0,Y = 1,Z = 2;
    final int TITLE = 30, STATUS = 40;
    final static int size = 2;
    Image myPic;

    private boolean gameNotOver = true;
    private boolean keyPressed = false;
    private Image myScreen;
    private int whichScreen;
    private int numScreen;
    private int timer;
    private int amt;

    public Clicker()
    {       
        setSize(SCWIDTH,SCHEIGHT);
        addWindowListener(new WindowAdapter()
              {
                  public void windowClosing(WindowEvent e)
                    {
                      System.exit(0);
                    }
               });

       this.setVisible(true);


       gameLoop();
    }
public void gameLoop()
{   
    do   
     {
        if (keyPressed)
           {
             amt++;
             out.println(keyPressed + " " +gameNotOver+" "+ "Time "+timer);
             this.repaint();
             keyPressed = false;
           }
        if(amt >= 450)
          gameNotOver = false;
        this.setVisible(true);
        //this.repaint();
        pause(30);
     }
   while (gameNotOver);     
   if(!gameNotOver)
    {
        pause(5000);
        System.exit(0);
    }
}
public int getAmt()
{
    return amt;
}
public void paint(Graphics pen)
 {    
        if(whichScreen==0)
            {
                pen.drawImage(myPic,100,200,506,279,this);   //Draws the image
                pen.setFont(new Font("Timesroman", Font.ITALIC, 50));
                pen.drawString("Welcome to the Clicker Test", 200, 75);
                pen.setFont(new Font("Timesroman", Font.ITALIC, 40));
                pen.drawString("Created by Cody Coulter",150,150);
                pen.setFont(new Font("Timesroman", Font.ITALIC, 25));
                pause(2000); // 2000 final          
                whichScreen++;
            }
        else
            {
                myScreen =createImage(getSize().width,getSize().height);
                Graphics o = myScreen.getGraphics();
                doubleBuffer(o);
                pen.drawImage(myScreen,0,0,this);
            }
 }   
public void doubleBuffer(Graphics pen)  // Draws the window
 {     
          pause (500);
          numScreen++;
          if (numScreen > 0)          
               {      
                    setBounds(0,0,SCWIDTH,SCHEIGHT);
                    Color HPRed = new Color(213, 0, 0);
                    pen.setColor(Color.BLACK);
                    setTitle("Clicker Test  -- Cody Coulter."+
                             " To click, press the space bar");
                    pen.setFont(new Font("Timesroman",Font.PLAIN,48));
                    pen.drawString("Clicker Test ",350,75);
                    pen.setFont(new Font("TimesRoman",Font.BOLD,33));
                    pen.fillRect((SCWIDTH/4), (SCHEIGHT/3), 50, 500);

                    pen.setColor(HPRed);
                    pen.fillRect((SCWIDTH/4)-5, (SCHEIGHT/3)+5, 40, amt);


                    if(!gameNotOver)
                     {
                        if(amt > 100)
                        {
                            pen.setColor(HPRed);
                            pen.fillRect(SCWIDTH/4-75, SCHEIGHT/3, SCWIDTH/2+150, SCHEIGHT/3-60);
                            pen.setColor(Color.GRAY);
                            pen.fillRect(SCWIDTH/4-65, SCHEIGHT/3+10, SCWIDTH/2+130, SCHEIGHT/3-80);
                            pen.setColor(Color.YELLOW);
                            pen.setFont(new Font("Verdana", Font.BOLD, 40));
                            pen.drawString("You got it!", SCWIDTH/2-290, SCHEIGHT/2-20);
                        }
                     }
                    this.repaint();
               }
}    
public void keyPressed(KeyEvent e)
{
    keyPressed = true;
    setTitle(""+ KeyEvent.getKeyText(e.getKeyCode()));  
    System.out.println("hit + "+ KeyEvent.getKeyText(e.getKeyCode())+ " " + keyPressed);
    switch(e.getKeyCode())
       {
          case KeyEvent.VK_SPACE: amt= amt + 4;
                                  break; 
       }
}
public void mouseClicked(MouseEvent m)
{
}   
public void mouseEntered(MouseEvent m)
{
}   
public void mouseExited(MouseEvent m)
{
}   
public void mousePressed(MouseEvent m)
{
}   
public void mouseReleased(MouseEvent m)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}

public void update(Graphics G)
{
      paint(G);
}       
public static void pause (long r)
{
    try
       {
           Thread.sleep(r);
       }
    catch (Exception e) 
       {
           out.println(" sleep error " + e);
        }          
}
}

我是这个论坛董事会的新手,我很可能会使用任意信息来解决此特定问题,并且通常对编程还是新手。 任何见识,或如何缩小问题,将不胜感激。

再次说明的错误是“线程“主”中的异常” java.lang.NoClassDefFoundError:clicker(错误的名称:Clicker)

谢谢

public class clicker
{
    public static void main(String args[])
    {
        new Clicker();  //Make a window
    }

}

您实际上是两次宣布您的课程。 采取main功能并将其放在您的其他答题器类中,并删除小写字母。

class Clicker extends Frame   implements KeyListener, MouseListener
{
    public static void main(String args[])
    {
        new Clicker();  //Make a window
    }
    //.....rest of class.....
}

您的main()方法应放在Clicker类中。

所以解决方案是:

class Clicker extends Frame implements KeyListener, MouseListener
{
    public static void main(String args[])
    {
        new Clicker();  // Make a window
    }
...
}

另外,请确保您的文件名为Clicker.java ,以与您的类的名称匹配。

public Clicker(){       

setSize(SCWIDTH,SCHEIGHT);

    addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            System.exit(0);
        }
    });

    this.setVisible(true);

    //ADD THIS
    this.addKeyListener(this);
    this.addMouseListener(this);

    gameLoop();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM