Java 如何将小程序作为应用程序运行?

Java 如何将小程序作为应用程序运行?,java,applet,Java,Applet,我有一个Applet类,我想让它作为应用程序运行,所以我编写了以下代码: 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.s

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

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是applet类。如果我运行它,它工作得很好,但我要做的是让它作为一个应用程序运行

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

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类中的这一行:

danceMusic = getAudioClip(getCodeBase(), "../../audio/dance.au");
GetCodeBase()方法返回null,我需要关于如何使此方法正常工作的帮助,或者用另一个可能访问我的文件以获取资源的方法替换它


提前感谢您

使用Hangman.class.getResource()或getResourceAsStream()加载资源。
小程序有一个特殊的运行时环境。如果希望代码也作为应用程序运行,则不能依赖该环境提供的任何功能

显然,您在这里使用的是一个特定于小程序的函数。您必须搜索如何在应用程序中完成此操作,然后检测正在运行的环境并使用适当的方法

编辑:

我将尝试以下操作,而不是
getCodeBase()

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

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

我遇到了一个类似的问题,并提出了以下对参数、代码库和getImage非常有效的方法-我仍在寻找getAudioClip()部分。。。如果有像ImageIo这样的音频设备就太好了

// simulate Applet environment
Map<String, String> params = new HashMap<String, String>();
URL documentBase;

/**
 * set the parameter with the given name
 * 
 * @param name
 * @param value
 */
public void setParameter(String name, String value) {
    params.put(name.toUpperCase(), value);
}

@Override
public String getParameter(String name) {
    String result = null;
    if (params.containsKey(name))
        result = params.get(name);
    else {
        try {
            result = super.getParameter(name);
        } catch (java.lang.NullPointerException npe) {
            throw new IllegalArgumentException("parameter " + name + " not set");
        }
    }
    return result;
}

/**
 * @param documentBase
 *          the documentBase to set
 * @throws MalformedURLException
 */
public void setDocumentBase(String documentBase) throws MalformedURLException {
    this.documentBase = new URL(documentBase);
}

@Override
public URL getDocumentBase() {
    URL result = documentBase;
    if (result == null)
        result = super.getDocumentBase();
    return result;
}

@Override
public Image getImage(URL url) {
    Image result = null;
    if (this.documentBase != null) {
        try {
            result = ImageIO.read(url);
        } catch (IOException e) {
        }
    } else {
        result = super.getImage(url);
    }
    return result;
}
//模拟小程序环境
Map params=新的HashMap();
URL文档库;
/**
*使用给定名称设置参数
* 
*@param name
*@param值
*/
公共void setParameter(字符串名称、字符串值){
params.put(name.toUpperCase(),value);
}
@凌驾
公共字符串getParameter(字符串名称){
字符串结果=null;
if(参数containsKey(名称))
结果=params.get(名称);
否则{
试一试{
结果=super.getParameter(名称);
}捕获(java.lang.NullPointerException npe){
抛出新的IllegalArgumentException(“参数”+名称+“未设置”);
}
}
返回结果;
}
/**
*@param documentBase
*要设置的文档库
*@抛出错误的DurException
*/
public void setDocumentBase(字符串documentBase)引发畸形的异常{
this.documentBase=新URL(documentBase);
}
@凌驾
公共URL getDocumentBase(){
URL结果=文档库;
如果(结果==null)
结果=super.getDocumentBase();
返回结果;
}
@凌驾
公共图像getImage(URL){
图像结果=空;
如果(this.documentBase!=null){
试一试{
结果=ImageIO.read(url);
}捕获(IOE异常){
}
}否则{
结果=super.getImage(url);
}
返回结果;
}

首先,我想感谢您,但您能否引导我找到一种在小程序中不使用“getCodeBase”url的情况下加载图像和音频剪辑的正确方法?