如何将小程序作为应用程序运行? [英] how can i run an applet as an application?

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

问题描述

我有一个 Applet 类,我想让它作为应用程序运行,所以我写了以下代码:

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

注意:Hangman 是小程序类.如果我运行它,它工作正常,但我想做的是让它作为应用程序运行.

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

这个错误来自于 Hangman 类中的这一行:

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

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

GetCodeBase() 方法返回 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?

先谢谢你

推荐答案

Applet 有一个特殊的运行时环境.如果您希望您的代码也作为应用程序运行,那么您不能依赖该环境提供的任何功能.

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.

显然,您在这里使用的是Applet 特定的功能.您必须搜索如何在应用程序中完成此操作,然后检测您正在运行的环境并使用适当的方法.

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.

我会尝试:

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

但是 getAudioClip 也在小程序中定义,所以这也是不行的.您必须使用 javax.sound.sampled API,而不是 java.applet.AudioClip.

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.

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

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