2013-04-30 70 views
0

我已經使用Eclipse創建了一個java程序,並試圖在線運行它(這是一個簡單的遊戲)。我有自己的.class文件,程序運行端到端沒有問題。當我嘗試在網站上運行它時遇到問題。我用下面的代碼來嘗試,沒有運氣啓動它作爲一個applet:在網站上運行java類

<html> 
<body> 
<applet 
    code="Handler.class" 
    codebase="AlamoAdventure/" 
    name="Alamo Battle Adventure" 
    width="680" 
    height="509" 
    archive="Handler.jar" 
    id="Alamo Battle Adventure" > 
</applet> 
</body> 
</html> 

當我轉到它告訴我點擊的小程序詳細信息的網站。當我這樣做時,我得到一個彈出窗口,它給我一個RuntimeException:java.lang.reflect.InvocationTargetException

我錯過了什麼步驟讓它運行? Handler.class和Handler.jar都在相同的位置。

主菜單java代碼如下:

/* 
* Main Menu is the start up window that is displayed when the program starts. It has one button, "Start Game", and a randomly 
* selected picture from a batch of 6. 
* 0->23 
*/ 
import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.awt.image.BufferedImage; 
import java.awt.image.ImageObserver; 
import java.util.Random; 
import javax.swing.Box; 
import javax.swing.BoxLayout; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 

public class MainMenu extends JFrame 
{ 
    private JButton sGame; //The "Start Game" Button 
    private FlowLayout layout; //Layout of the window 
    private ImageIcon image; //The images 

    public MainMenu() 
    { 
     super("ABA:Main Menu"); // Title of Window 
     JPanel right = new JPanel(); 
     JPanel left = new JPanel(); 
     left.setLayout(new BoxLayout(left, BoxLayout.LINE_AXIS)); 
     add(left); 
     right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS)); 
     add(right); 

     sGame = new JButton("Start Game"); 
     left.add(sGame); 
     left.add(Box.createRigidArea(new Dimension(10,0))); 

     right.add(left); 

     Images image = new Images(); 
     right.add(image); 

     ButtonHandle handle = new ButtonHandle(); 
     sGame.addActionListener(handle); 
    } // end Button const. 
    /* 
    * When the button is pressed it will call the functions to start the new windows and storyline 
    */ 
    private class ButtonHandle implements ActionListener 
    { 
     public void actionPerformed(ActionEvent event) 
     { 
      if (event.getActionCommand().contentEquals("Start Game")) 
      { 
       int[] i; 
       int n, ch, c; 
       i = new int[3]; 
       Text story = new Text(); 

       int k = 10; 
       i = Options.Option1(k); 
       n = i[0]; 
       ch = i[2]; 
       c = i[1]; 

       /* 
       * Debugging technique to allow proper pathing for map and story line 
       */ 
       System.out.print(n); 
       System.out.print(c); 

       String option1; 
       String option2; 
       String option3; 
       String storyMode; 

       String[] a; 
       a = new String[2]; 
       a = Choices.getChoice(n); 

       storyMode = story.getText(ch,n); 

       option1 = a[0]; 
       option2 = a[1]; 
       option3 = a[2]; 


       Button call = new Button(option1, option2, option3, storyMode, n); 
       call.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       call.setSize(680,509); 
       call.setVisible(true); 
       MainMenu.this.dispose(); 
      } 

     } 
    } 
    /* 
    * Class to render the 6 random pictures on the Main Menu under the "Start Game" 
    */ 
    class Images extends JPanel 
    { 
     private ImageIcon picture; 
     private Random generator = new Random(); // Generator for random numbers 
     private String[] images = {"1.JPG", "2.JPG", "3.JPG", 
      "4.JPG", "5.JPG", "6.JPG"}; // These are the paths to the 6 pictures that will be randomly called 

     public Images() 
     { 
      int randomN = generator.nextInt(images.length); // Random number between 0 and 5 
      picture = new ImageIcon(getClass().getResource(images[randomN])); // initializes the picture and selects the path from the randomN 

     } 

     protected void paintComponent(Graphics g) 
     { 
      super.paintComponents(g); 
      Dimension d = getSize(); 
      g.drawImage(picture.getImage(),0,0,d.width, d.height, null); 

     } 
     public Dimension getPreferredSize() 
     { 
      return new Dimension(picture.getIconWidth(), picture.getIconHeight()); 
     } 
    } 

} 
+0

你確定你寫的課程不在包裝下嗎? – aksappy 2013-04-30 06:02:09

+0

而且..該URL又在哪裏?順便說一句 - 代碼中的類聲明是什麼? – 2013-04-30 06:06:08

+0

http://www.kluckhohndesign.com/test – 2013-04-30 14:47:36

回答

1

一個小測試表明,這裏的類不是一個小程序。當控制檯設置爲5級,並且applet運行時,細節就在那裏。我希望你錯誤地做出了JFrame,但無論如何這可能會更好。

發佈該類聲明,以便我可以確認。


當從命令行運行,我看到:

Exception in thread "main" java.lang.NullPointerException 
     at javax.swing.ImageIcon.<init>(Unknown Source) 
     at MainMenu$Images.<init>(MainMenu.java:120) 
     at MainMenu.<init>(MainMenu.java:46) 
     at Handler.main(Handler.java:11) 

什麼是對的MainMenu.java 120線?

+0

我已經添加了MainMenu.java編碼 – 2013-04-30 14:56:44

+0

行120調用圖像,它是一個Jframe而不是一個小程序,有沒有什麼辦法讓它運行該網站沒有把所有東西都變成一個applet? – 2013-04-30 16:06:08