我怎么能运行applet作为一个应用程序? [英] how can i run an applet as an application?

查看:120
本文介绍了我怎么能运行applet作为一个应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Applet类的,我希望把它作为应用程序运行,所以我写了下列code:

I have an Applet class, and I want to make it run as an application, so I wrote to following code:

public static void main(String args[]) {
JFrame app = new JFrame("Applet Container");
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(200, 100);
Hangman applet = new Hangman();
applet.init();
app.setLayout(new BorderLayout());
app.setSize(500,500);
app.getContentPane().add(applet, BorderLayout.CENTER);
app.setVisible(true);
}

请注意:刽子手是applet类。如果我运行它,它做工精细,但我所特林做的,是让作为应用程序运行。

Note: Hangman is the applet class. And if i run it, it work fine, but what I am tring to do, is to make it run as an application.

当我运行上面的主,我得到了以下错误:

When I run the above main, I got the following error:

Exception in thread "main" java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Applet.java:152)
at Me.Hangman.init(Hangman.java:138)
at Me.Client.main(Client.java:54)
Java Result: 1

此错误是来自于刽子手类这一行:

This error is came from this line in the Hangman class:

danceMusic = getAudioClip(getCodeBase(), "../../audio/dance.au");

得到codeBase的()方法返回null,我需要我怎样才能使这种方法正常工作,或者与可能访问我的文件以获得资源的另一种方法取代它的帮助?

GetCodeBase() method returns null, I need help on how can I make this method work properly, or maybe replace it with another method that may access my files to get resources?

感谢您提前

推荐答案

小程序有一个特殊的运行时环境。如果你希望你的code也作为应用程序运行,那么你可以不依赖于由环境所提供的任何功能。

Applets have a special runtime environment. If you want your code to run also as an application then you can not rely on ANY functionality provided by that environment.

显然,你在这里使用的小程序特异功能。你必须寻找如何得到这个应用程序中完成的,那么检测到正在运行的环境和使用适当的方法。

Obviously you are using an Applet-specific function here. You have to search for how to get this done in an application, then detect the environment you are running in and use the appropriate methods.

编辑:

而不是得到codeBase的()我会尝试:

getClass().getProtectionDomain().getCodeSource().getLocation();

getAudioClip 在一个applet也定义的,所以这是一个不走了。取而代之的 java.applet.AudioClip中,你必须使用的javax.sound.sampled API。

But getAudioClip is also defined in an applet, so that is a no-go too. Instead of java.applet.AudioClip you have to use the javax.sound.sampled API.

这篇关于我怎么能运行applet作为一个应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆