2015-06-05 5 views
1

Я создал простую игру Minesweeper с использованием Java в Eclipse. Я использовал обычную банку (а не runnable jar) для создания файла jar.Classnotfound Exception при встраивании jar с HTML

Я не использовал init() или любые такие функции.

Вот весь код:

import java.applet.Applet; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.util.ArrayList; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 


public class Minesweeperapplet extends Applet implements ActionListener { 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 

JFrame frame= new JFrame("Minesweeper von Junaid Aslam"); 

JButton reset = new JButton("Reset"); //declaring reset button - deklaration des 'reset' button 

JButton [][]buttons= new JButton[20][20]; //declaring jbuttons for the game - declaring echt buttons fur das spiel 

Container grid= new Container(); //container for the grid - Container fur das grid 

boolean [][] zero= new boolean[20][20]; //to keep track of all uncovered zeros - um darauf die aufgedeckten zeros zu markieren 

int[][] counts = new int[20][20]; //holds the integer count of all neighbouring mines - das Feld beeinhaltet die Informationen uber die Nummer der angrenzenden Minen 

final int mine=10; //Final value set for a mine - diese 'final' Variable representiert eine Mine 

int minesSetForGame = 35;//Number of mines in the game - Bestimmung uber die Anzahl der Minen 

public Minesweeperapplet(){ 


    frame.setSize(1000,700); 
    frame.setLayout(new BorderLayout()); 
    frame.add(reset, BorderLayout.NORTH); //inserting reset button in the borderLayout - Hinzufugen des reset buttons zum borderlayout 


    reset.addActionListener(this); 


    //setting up grid - stellen die Grid ein 
    grid.setLayout(new GridLayout(20,20)); 

    //initialize button grid - initialisierung des button grid 
    for(int a=0;a<buttons.length;a++) 
    { 
     for(int b=0; b<buttons[0].length;b++) 
     { 
      buttons[a][b]= new JButton(); 
      buttons[a][b].addActionListener(this); //listener for left click - Zuhoren auf 'Left Click' 
      buttons[a][b].addMouseListener(new Mouse());//listener for right click - Zuhoren auf 'Right Click' 
      grid.add(buttons[a][b]); //adding buttons to the grid - Hinzufugen der buttons zum grid 

     } 

    } 

    frame.add(grid, BorderLayout.CENTER); //adding the grid to layout - etablieren des grids in der Mitte 
    creatRandomMines(); //creating random mines - Erstellen zufalliger Minen 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
} 



public void creatRandomMines(){ //Creating random mines - Erstellen zufalliger Minen 

    ArrayList<Integer> list= new ArrayList<Integer>(); 

    //initialize list of random pairs 
    for(int x=0; x < counts.length; x++) 
    { 
     for(int y=0; y < counts[0].length; y++) 
     { 
      list.add(x*100+y); 

     } 
    } 

    //reset count and pick 30 mines 
    counts= new int[20][20];//for reset 

    for(int a=0; a<minesSetForGame; a++){ 

     int choice= (int)(Math.random()*list.size());//Math.random()*list.size() picks out number from 0 to 399(400=list.size()) 
     counts[list.get(choice)/100][list.get(choice)%100]= mine; 
     list.remove(choice);//removing the choice we have made for the mine 
    } 

    //initializing count for counting neighbouring mines- Initialliesieren der Berechnung von den Minen in den Nachbarfeldern 
    for(int x=0;x<counts.length;x++) 
    { 
     for(int y=0; y<counts.length; y++) 
     { 
      if(counts[x][y]!=mine) 
      { 
      int neighborcount=0; 
      if(x>0 && y>0 && counts[x-1][y-1]==mine)//Oben links button 
      { 
       neighborcount++; 
      } 
      if(y>0 && counts[x][y-1]==mine)//Links button 
      { 
       neighborcount++; 
      } 
      if(y<(counts[0].length-1) && counts[x][y+1]==mine) //rechts button 
      { 
       neighborcount++; 
      } 
      if(x>0 && y<(counts[0].length-1) && counts[x-1][y+1]==mine)//Oben rechts 
      { 
       neighborcount++; 
      } 
      if(x>0 && counts[x-1][y]==mine)//Oben 
      { 
       neighborcount++; 
      } 
      if(x<(counts.length-1) && y<(counts[0].length-1) && counts[x+1][y+1]==mine)//unten rechts 
      { 
       neighborcount++; 
      } 
      if(x<(counts.length-1)&& y>0 && counts[x+1][y-1]==mine)//unten links 
      { 
       neighborcount++; 
      } 
      if(x<(counts.length-1) && counts[x+1][y]==mine)//unten 
      { 
       neighborcount++; 
      } 

      counts[x][y]=neighborcount; 
      } 
     } 
    } 

} 

public void lost(){//in case we lose the game - Wenn das Spiel verloren ist, zeige alle Buttons 

    for(int x=0;x<buttons.length;x++) 
    { 
     for(int y=0;y<buttons[0].length;y++) 
     { 
      if(counts[x][y]!=mine) 
      { 
      buttons[x][y].setText(counts[x][y]+""); 
      buttons[x][y].setEnabled(false); //Machen setEnabled=false fur alle nicht Minen buttons 
      } 
      else{ 
       buttons[x][y].setBackground(Color.ORANGE);//Markiere alle immer noch zugedeckten Minen orange 
       buttons[x][y].setText("X");//Markiere alle immer noch zugedeckten Minen mit "X" String 


      } 
     } 
    } 
} 

public void zero(int x, int y){ //decke alle angrenzenden zeros auf 


    if(zero[x][y]==false) 
    { 
      buttons[x][y].setEnabled(false); 
      buttons[x][y].setText(counts[x][y]+""); 
      zero[x][y]=true; //alle aufgedeckten zeros werden 'true' gemacht 

      //aufdecken der felder bis zum 'non-zero'(zahlen) button 

      if(x<(counts.length-1)){ 
      buttons[x+1][y].setText(counts[x+1][y]+""); //fur unten buttons so that they keep opening until the number is not 0 
      buttons[x+1][y].setEnabled(false);} 

      if(x>0) 
      {buttons[x-1][y].setText(counts[x-1][y]+"");//oben 
      buttons[x-1][y].setEnabled(false);} 

      if(y>0){ 
      buttons[x][y-1].setText(counts[x][y-1]+"");//links 
      buttons[x][y-1].setEnabled(false);} 

      if(y>0 && x>0){ 
      buttons[x-1][y-1].setText(counts[x-1][y-1]+"");//links oben 
      buttons[x-1][y-1].setEnabled(false);} 

      if(x>0 && y<(counts[0].length-1)){ 
      buttons[x-1][y+1].setText(counts[x-1][y+1]+"");//rechts oben 
      buttons[x-1][y+1].setEnabled(false);} 

      if(y<(counts[0].length-1)){ 
      buttons[x][y+1].setText(counts[x][y+1]+"");//rechts button 
      buttons[x][y+1].setEnabled(false);} 

      if(x<(counts.length-1) && y>0){ 
      buttons[x+1][y-1].setText(counts[x+1][y-1]+"");//links unten 
      buttons[x+1][y-1].setEnabled(false);} 

      if(x<counts.length-1 && y<counts[0].length-1){ 
      buttons[x+1][y+1].setText(counts[x+1][y+1]+"");//rechts unten 
      buttons[x+1][y+1].setEnabled(false);} 

      //to clear out all zeros adjacent to zeros already cleared out 
      //um alle zeros aufzudecken, die an bereits auf gedeckte zeros angrenzen 

      if(x>0 && counts[x-1][y]==0)//oben - Upper button 
      { 
       zero(x-1,y); 
      } 
      if(x<(counts.length-1) && counts[x+1][y]==0)//unten - Lower button 
      { 
       zero(x+1,y); 
      } 
      if(y>0 && counts[x][y-1]==0)//links - Left button 
      { 
       zero(x,y-1); 
      } 
      if(y>0 && x>0 && counts[x-1][y-1]==0)//links oben - upper left button 
      { 
       zero(x-1,y-1); 
      } 
      if(x>0 && y<(counts[0].length-1) && counts[x-1][y+1]==0)//rechts oben - right button 
      { 
       zero(x-1,y+1); 
      } 
      if(y<(counts[0].length-1) && counts[x][y+1]==0)//rechts - right button 
      { 
       zero(x,y+1); 
      } 
      if(x<(counts.length-1) && y>0 && counts[x+1][y-1]==0)//links unten - lower left 
      { 
       zero(x+1,y-1); 
      } 
      if(x<counts.length-1 && y<counts[0].length-1 && counts[x+1][y+1]==0)//rechts unten - lower right 
      { 
       zero(x+1,y+1); 
      } 
    } 
} 


    public void checkforwin() //um zu Kontrollieren ob spieler gewonnen hast 
           //to check if player has won 
     { 
     int minecounter=0,nonminecounter=0; 

     for(int x=0;x<buttons.length;x++) 
      { 
       for(int y=0;y<buttons[0].length;y++) 
       { 

        if(counts[x][y]==mine) 
        { 

        if(buttons[x][y].isEnabled()) 
        { 
         minecounter++; //Wird alle Minen zahlen, die noch nicht angeklickt wurden 
        }     //will count all mines which have not been clicked on 
        } 
        else if(counts[x][y]!=mine) 
        { 
         if(!buttons[x][y].isEnabled()) 

          nonminecounter++; // will count all buttons other than mines which have been shown to the player or clicked on 
               //Wird alle buttons zahlen, die aufgedeckt sind und keine minen sind 
        } 
       } 

       if(minecounter==minesSetForGame && nonminecounter==((counts.length*counts[0].length)-minecounter)) 
       {//checks if all mines are isEnabled(not clicked on) and all other buttons are !isEnabled()(they are disabled) 
        JOptionPane.showMessageDialog(new JFrame(), "Sie haben Gewonnen."); 
       } 

    } 

} 

@Override 
public void actionPerformed(ActionEvent event) { 

    if(event.getSource().equals(reset)){ 


     for(int x=0;x<buttons.length;x++) //resets all buttons[x][y] 
     {         
      for(int y=0;y<buttons[0].length;y++) 
      { 
       buttons[x][y].setEnabled(true); 
       buttons[x][y].setBackground(null);//sets all button background color to default 
       buttons[x][y].setForeground(null); 
       buttons[x][y].setText(""); 
      } 
    } 
     creatRandomMines();//creates mines for restart - erstellt Minen fur den Neustart 
     zero=new boolean[20][20]; //machen alle zero zu false noch mal 

    }else{ 

     for(int x=0; x<buttons.length; x++)//ob der Spieler druckt ein andere button 
     { 
      for(int y=0; y<buttons[0].length; y++) 
      { 
       if(event.getSource().equals(buttons[x][y])) 
       { 
        if(buttons[x][y].getBackground().equals(Color.red) || buttons[x][y].getBackground().equals(Color.ORANGE)) 
        { 
         //if the button is flagged or has orange background (after losing) do nothing 
         //Wenn der button 'flagged' ist, konnen wir nichts machen 
        } 
        else if(counts[x][y]==mine)  // if the button is a mine - wenn der angeklickte Button eine Mine ist 
        { 
         lost();//calling lost method because game is lost - das spiel ist verloren 
         buttons[x][y].setForeground(Color.red); 
         buttons[x][y].setText("X"); 



        }else if(counts[x][y]==0)// if the clicked button is zero 
        {      //wenn der angeklickte button zero ist 
         int i=x,j=y; 
         zero(i,j); //calling zero method 
        }    //rufen zero method 


        else{ 
        buttons[x][y].setText(counts[x][y]+"");//sets the clicked button to count[x][y]- einsetzen der nummer bei einem eingeklickten button zu count[x][y] 
        buttons[x][y].setEnabled(false); //disables the button after the click - 'disable' button nach dem Klick 
        checkforwin();//checking for winner after a non-mine button is clicked - prufen der gewinner 

        } 
       } 
      } 
     } 
    } 

} 

public class Mouse extends MouseAdapter { //for right click functionality 
              // fur rechten Klick Funktionalitat 

    @Override 
    public void mousePressed(MouseEvent event){ 
     if(event.isMetaDown()) 
     { 
      for(int x=0;x<buttons.length;x++) 
      { 
       for(int y=0;y<buttons[0].length;y++) 
       { 
        if(event.getSource().equals(buttons[x][y])) 
        { 
         if(!buttons[x][y].getBackground().equals(Color.red))// if the button is not yet flagged - wenn die button noch nicht 'flagged' ist 
         { 
          if(buttons[x][y].isEnabled()) //to make sure the button is still enabled - um zu prufen dass der button isEnabled() ist 
          { 
           buttons[x][y].setBackground(Color.red);//set color to red if clicked once - stelle die Background farbe Rot ein bei einem Klick 

          } 

         } 
         else{ 
          buttons[x][y].setBackground(null);//set color to default if clicked again - stelle die farbe zu Default bei noch einem klick 
          } 

        } 
       } 
     } 


     } 
    } 


} 

} 

Вот мой полный HTML код:

<!DOCTYPE html> 
    <html> 
    <head> 
<title>Minesweeper100</title> 


</head> 

<body> 


    <applet code = "Minesweeperapplet.class" archive="Minesweeperapplet.jar" width=1000 height=700></applet> 

</body> 

</html> 

Теперь я использую сервер Filezilla загрузить и запустить его на Firefox. Он говорит "ClassNotFoundException Minesweeperapplet.class" ... каждый раз, когда я запускаю его.

, Jar:

Я застрял на нем в течение 2-х дней. Здесь я прочитал все такие вопросы, но моя проблема, похоже, не исчезла. HTML и jar находятся в одной папке; все имена файлов были проверены и переустановлены на миллион раз. Я даже мечтал об этой ошибке.

ПОЖАЛУЙСТА, ПОМОГИТЕ !!!!!

ответ

1

Используйте имя класса не имя класса файла для значения code: Minesweeperapplet

+0

Пробовал это же ошибка минус .class –

+0

Вы пробовали положить свой класс в пакете? – nitind

+0

Я нашел проблему ... это было с fillzilla, у нее была проблема, но с меняющимися настройками java она работала. –

Смежные вопросы