在网站上运行java类

时间:2013-04-30 05:47:27

标签: java html applet invocationtargetexception

我使用Eclipse创建了一个java程序,并试图在线运行它(这是一个简单的游戏)。我有.class文件自己工作,程序端到端运行没有问题。当我尝试在网站上运行时遇到问题。我已经使用以下代码尝试将其作为小程序启动而没有运气:

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

}

1 个答案:

答案 0 :(得分:1)

这里的一点测试表明,类不是applet。当控制台设置为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行是什么?