简体   繁体   English

java applet ExceptionInInitializerError

[英]java applet ExceptionInInitializerError

I'm new to programming with Applets and wanted to make an Applet to put on a website. 我是使用Applet编程的新手,并且想制作一个Applet放在网站上。 So here we go. 所以我们开始。

The Goal of this project was that if you click the button, it will open a JFrame on top of the browser. 该项目的目标是,如果单击按钮,它将在浏览器顶部打开一个JFrame。 but while testing, it gives me a java.lang.ExceptionInInitializerError . 但是在测试时,它给了我一个java.lang.ExceptionInInitializerError This is the source code: 这是源代码:

public class LaunchMenu extends Applet {

    public static LoginScreen login;
    public static Game game;
    public JButton button;
    public void init() {
        try {button= new JButton("Press this button to start");
        add(button);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                login = new LoginScreen();

            }
        });
        } catch (Exception e) {
            e.getMessage();
            e.getCause();
        }
    }

    public void start() {
        login = new LoginScreen();
    }

    public void stop() {
        login.dispose();
        game.stop();
    }

    public static void main(String[] args) {

    }

}

note: It works in Eclipse with it's Applet window, but not on the website. 注意:它的Applet窗口在Eclipse中有效,但在网站上不起作用。

edit: 编辑:

Exception in thread "AWT-EventQueue-2" java.lang.IllegalStateException: Applet's parent container not set up
    at sun.plugin2.applet.Plugin2Manager.start(Unknown Source)
    at sun.plugin2.main.client.PluginMain$StartAppletRunner.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
CacheEntry[http://localhost/AppletTest/Applet.jar]: updateAvailable=true,lastModified=Mon Apr 13 12:24:52 CEST 2015,length=5051938

Ok I fixed my problem. 好吧,我解决了我的问题。 Here's the solution that worked for me. 这是对我有用的解决方案。

SourceCode stays the same (except for a minor change )* : SourceCode保持不变(除了较小的更改 )*:

public class LaunchMenu extends Applet {

    public static LoginScreen login;
    public static Game game;
    public JButton button;
    public void init() {
        try {button= new JButton("Start the game");
        add(button);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                login = new LoginScreen();
                login.setVisible(true); *

            }
        });
        } catch (Exception e) {
            e.getMessage();
            e.getCause();
        }
    }

    public void start() {
        login = new LoginScreen();
    }

    public void stop() {
        login.dispose();
        game.stop();
    }

    public static void main(String[] args) {

    }
}

But I added a java.policy file in the same folder as the Applet. 但是我在与Applet相同的文件夹中添加了一个java.policy文件。 Within this file I wrote the following code: 在此文件中,我编写了以下代码:

grant { 
      permission java.security.AllPermission; 
}; 

After this was setup, I ran in another problem named java.lang.RuntimePermission: "exitVM.0" 设置java.lang.RuntimePermission: "exitVM.0"之后,我遇到了另一个名为java.lang.RuntimePermission: "exitVM.0"问题java.lang.RuntimePermission: "exitVM.0"

The solution to this problem was simple. 解决这个问题的方法很简单。 In the class with my JFrame, in my case LoginScreen , there was a line code setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 在带有JFrame的类中,以我的情况为LoginScreen ,有一个行代码setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); . Delete this or comment it, this worked for me and now my Applet is visible and the Login frame opens. 删除它或对其进行评论,这对我有用,现在可以看到我的Applet并打开“登录”框架。 which was all I needed. 这就是我所需要的。

Hope this helps alot of people with the same problem. 希望这可以帮助很多有相同问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM