2014-02-25 72 views
1

我創建一個基本的程序,它是做一個「生活吧」,當你按下空格鍵填充,當你到了最大,你贏了。每秒鐘它減少,所以只是空格空格鍵贏得勝利。它試圖通過java來測試一個基本的遊戲。我的程序被捕獲到這個錯誤,給它一個我不熟悉的運行時錯誤。這裏是我的代碼:爲什麼我得到NoClassDefFoundError?

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

我是新來這個論壇主板,而我最有可能使用任意的信息來解決這方面的問題,一般是新來的節目。任何見解,或如何縮小問題將不勝感激。

再次,指出錯誤是「異常線程 「main」 java.lang.NoClassDefFoundError:唱首歌(錯誤名稱:唱首歌)

謝謝

+2

什麼是Java源文件的文件名? –

+2

在一個側面說明:類名應該總是以一個大寫字母開頭,所以有'clicker'和'Clicker'是不是好的做法。 – Tyler

+0

我把'public class clicker'(看案例)改爲'public class MyClicker'並且工作。首先,我能夠重新創建錯誤。我正在研究代碼爲什麼修復。 – makata

回答

0
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..... 
} 
+0

我明白你的意思,但是當我試圖重新編譯爲你推薦的,它給一個錯誤,說明找不到主類:唱首歌。它是否保存爲clicker.java? – Munkeeface

0

main()實現方法具d應放置在Clicker班。

因此,解決辦法是:

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

此外,請確保您的文件被命名爲Clicker.java,以配合您的類名。

+0

其實你可以但只有一個可以公開! –

+0

「你不能定義在一個單一的文件2 Java類」'公共類Foo {}類酒吧{}'似乎是在編譯'Foo.java'罰款。 – Pshemo

+0

因爲關於能見度的細節,不知道,對不起。聲明已刪除。 – Flawyte

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(); 
} 
相關問題