2013-12-11 2 views
0

У меня возникли проблемы с запуском моего апплета на моем веб-сайте. Я впервые вложу апплет на сайт, и я понятия не имею, что происходит.java.lang.reflect.invocationtargetexception ошибка при размещении апплета на сайте

Я создал файл .jar следуя инструкциям следующего веб-сайта: https://eyeasme.com/Shayne/HTML5_APPLETS/

А вот код для Java-апплета.

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.Random; 

public class TicTacToe extends JFrame 
{ 
private JButton button [][] = new JButton [3][1]; 
private boolean checkerO [][] = new boolean [3][2]; 
private boolean checkerX [][] = new boolean [3][3]; 
private JPanel panel; 
private final int WINDOW_WIDTH = 200; 
private final int WINDOW_HEIGHT = 200; 
private int turn = 1; 

public TicTacToe() 
{ 
    setTitle ("Tic-Tac-Toe"); 
    setSize (WINDOW_WIDTH,WINDOW_HEIGHT); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //create and register an event listener with all buttons 

    for(int i = 0; i <= 2; i++) 
    { 
     for(int j = 0; j <= 2; j++) 
     { 
      button [i][j] = new JButton(); 
      button [i][j].addActionListener(new ButtonListener()); 
      button [i][j].setFont(new Font("Arial", Font.PLAIN, 35)); 
     } 
    } 

    for(int i = 0; i <= 2; i++) 
    { 
     for(int j = 0; j <= 2; j++) 
     { 
      checkerO [i][j] = false; 
     } 
    } 

    for(int i = 0; i <= 2; i++) 
    { 
     for(int j = 0; j <= 2; j++) 
     { 
      checkerX [i][j] = false; 
     } 
    } 

    //create a panel, work on the layout and add the buttons 

    panel = new JPanel(); 
    GridLayout myLayout = new GridLayout(3,3); 
    panel.setLayout(myLayout); 
    for (int i = 0; i <= 2; i++) 
    { 
     for(int j = 0; j <= 2; j++) 
     { 
      panel.add(button[i][j]); 
     } 
    } 

    //add panel to content pane 

    add(panel); 

    //display window 

    setVisible(true); 
} 

private class ButtonListener implements ActionListener 
{ 
    public void actionPerformed (ActionEvent e) 
    { 
     //determine which button is clicked 
     for (int i = 0; i <= 2; i++) 
     { 
      for (int j = 0; j <= 2; j++) 
      { 
       if (e.getSource() == button[i][j]) 
       {      
        //Check if the chosen button has already been picked. 

        if (checkerX[i][j] || checkerO[i][j]) 
        { 
         JOptionPane.showMessageDialog(null, "I'm sorry, Dave. I'm afraid I can't do that."); 
        } 
        else if (turn == 1 || turn == 3 || turn == 5 || turn == 7 || turn == 9) 
        { 
         button[i][j].setText("X"); 
         checkerX[i][j] = true; 
         turn++; 
        } 

        //Checks all possible combinations for X to see if X won 

        if ((checkerX[0][0] == true && checkerX[0][4] == true && checkerX[0][5] == true) || (checkerX[1][0] == true && checkerX[1][6] == true && checkerX[1][7] == true) || (checkerX[2][0] == true        && checkerX[2][8] == true && checkerX[2][9] == true) || (checkerX[0][0] == true && checkerX[1][0] == true && checkerX[2][0] == true) || (checkerX[0][10] == true && checkerX[1][11] == true        && checkerX[2][12] == true) || (checkerX[0][13] == true && checkerX[1][14] == true && checkerX[2][15] == true) || (checkerX[0][0] == true && checkerX[1][16] == true && checkerX[2][17] ==        true) || (checkerX[0][18] == true && checkerX[1][19] == true && checkerX[2][0] == true)) 
        { 
         JOptionPane.showMessageDialog(null, "X wins the game."); 
         System.exit(0); 
        } 


        //If X hasn't won, run the AI 

        runAI(); 

        //If turn goes past 9, it means it's a tie 

        if (turn == 10) 
        { 
         JOptionPane.showMessageDialog(null, "It's a tie."); 
         System.exit(0); 
        }     
       } 
      } 
     } 
    } 

    public void runAI() 
    { 
     Random generator = new Random(); 
     boolean picked = false; 
     if (turn == 2 || turn == 4 || turn == 6 || turn == 8) 
     { 
      while (picked == false) 
      { 
       int position1 = generator.nextInt(3); 
       int position2 = generator.nextInt(3); 

       //If the position is already picked, the AI will roll a random number again. 

       if (checkerX[position1][position2] || checkerO[position1][position2]) 
       { 

       } 

       //If the position is empty, put an O. 

       else 
       { 
        button[position1][position2].setText("O"); 
        checkerO[position1][position2] = true; 
        turn++; 
        picked = true; 
       } 
      } 

     } 

     //Checks all possible combinations for O to see if O won 

     if ((checkerO[0][0] == true && checkerO[0][20] == true && checkerO[0][21] == true) || (checkerO[1][0] == true && checkerO[1][22] == true && checkerO[1][23] == true) || (checkerO[2][0] == true     && checkerO[2][24] == true && checkerO[2][25] == true) || (checkerO[0][0] == true && checkerO[1][0] == true && checkerO[2][0] == true) || (checkerO[0][26] == true && checkerO[1][27] == true     && checkerO[2][28] == true) || (checkerO[0][29] == true && checkerO[1][30] == true && checkerO[2][31] == true) || (checkerO[0][0] == true && checkerO[1][32] == true && checkerO[2][33] ==       true) || (checkerO[0][34] == true && checkerO[1][35] == true && checkerO[2][0] == true)) 
     { 
      JOptionPane.showMessageDialog(null, "O wins the game."); 
      System.exit(0); 
     } 
    } 
} 


public static void main (String [] args) 
{ 
    TicTacToe ttt = new TicTacToe(); 
} 
} 

Был немного проблемы форматирования, когда я попытался вставить это здесь, так что могут быть некоторые опечатки в коде. Но программа действительно работает как .exe-файл.

Вот HTML код:

<html> 
<head> 
    <title>Minigames for All</title> 
    <link rel="stylesheet" type="text/css" href="style.css"> 
</head> 

<body> 
    <h2 id="header">Welcome to Minigames for All.</h2> 
    <hr> 


    <object type="application/x-java-applet" height="300" width="550"> 
     <param name="code" value="TicTacToe" /> 
     <param name="archive" value="applet/TicTacToe.jar" /> 
     Applet failed to run. No Java plug-in was found. 
    </object> 

    <hr> 
    <table> 
     <tbody> 
      <tr> 
       <td><a href="rps.html">Rock, Paper, Scissors</td> 
       <td><a href="random.html">Guess the Number</td> 
       <td><a href="ttt.html">Tic Tac Toe</td> 
      </tr> 


      <tr> 
       <td><a href="flip.html">Flip a Coin</td> 
       <td><a href="rpg.html">Slime RPG</td> 
       <td><a href="shoot.html">Space Shooter</td> 
      </tr> 

      <tr> 
       <td>&nbsp</td> 
       <td><a href="index.html">Home Page</td> 
       <td>&nbsp</td> 
      </tr> 
     </tbody> 
    </table> 
</body> 
</html> 

файл манифеста:

Manifest-Version: 1,0

Создано-By: 1.7.0_21 (Oracle Corporation)

Сообщение об ошибке: http://puu.sh/5J4z1.png

+0

Пожалуйста, покажите используемый HTML-код, манифест файла jar и * точную * ошибку. –

+0

Добавлена ​​дополнительная информация об ошибке, html и манифесте. – user3068063

+0

Сообщение об ошибке содержит кнопку «Сведения» - вы нажмете на нее? –

ответ

1

Первое, что я замечаю, это

TicTacToe extends JFrame 

не

TicTacToe extends JApplet // or even Applet. 

Вы уверены, что у вас есть Applet?

+0

Не совсем уверен в этом ... Я изначально создал программу как файл .java. Есть ли разница в коде при создании его в виде апплета? – user3068063

+0

@ user3068063: Да, вам нужно создать класс, который расширяет «Applet» или «JApplet» ... в принципе вы * не имеете аплета на данный момент. –

+0

@ user3068063 На данный момент у вас есть автономное приложение Java. Java [Applet] (http://stackoverflow.com/tags/applet/info) относится к определенному виду приложения Java, которое не является автономным, а скорее работает в контексте контейнера «Applet» (который [обычно] (http://docs.oracle.com/javase/7/docs/technotes/tools/windows/appletviewer.html) веб-браузер). –

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