为什么我得到NoClassDefFoundError?

时间:2014-02-25 15:10:10

标签: java

我正在创建一个基本程序,即当你按空格键时会生成一个“生命栏”,当你达到最大值时,你就赢了。它每秒都会减少,所以只需垃圾邮件即可赢得空格。它试图通过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)

谢谢

3 个答案:

答案 0 :(得分:0)

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

}

你基本上是在宣告你的班级两次。取main函数并将其放入另一个clicker类中,然后删除小写的一个。

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

答案 1 :(得分:0)

main()类应放置Clicker方法。

所以解决方案是:

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

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

答案 2 :(得分:0)

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();
}