Loading a .Jar Applet inside of a JFrame(Panel)

流过昼夜 提交于 2019-12-02 15:03:15

问题


I'm trying to make a JFrameable "Loader" of one of my favorite Java games, but I don't know how to load the actual .Jar into a JFrame panel (I was told this was called an Applet, guess I'm behind a little bit)

I have the JFrame set up with panels and everything where I want it, but I have no idea how to go about loading the .jar and sending parameters to it and telling it where i want it to be at.

Any help or links would be greatly appreciated... as I can't find anything


回答1:


Since you haven't provided the site which is being used I'll only explain some parts.

Firstly you'll need to download the games .JAR file or have it accessible.

Secondly you'll need to have knowledge of the main class typically it's called "main.class".

Thirdly you'll need code similar to the below to load the .jar files main file. Once again changing the "main.class" variable to the actually ".class".

URL url[] = { 
    new File(directory.concat("/gamepack.jar")).toURI().toURL() 
};

URLClassLoader classLoader = new URLClassLoader(url);
applet = (Applet)classLoader.loadClass("main").newInstance(); 
applet.setBounds(0, 0, width, height);

applet.setBackground(Color.BLACK);
applet.setStub(stub);

applet.init();
applet.start();

mainFrame.getContentPane().add(applet);

You'll notice the method "applet.setStub(stub)". You'll need to create an AppletStub class. Something similar to do the below will suffice.

package com;

import java.util.Map;
import java.util.HashMap;
import java.net.URL;
import java.applet.AppletStub;
import java.applet.AppletContext;

public class AppletEnvironment implements AppletStub
{
private final Map<String, String> PARAMETERS = new HashMap<String, String>();
private final URL URLBASE;

public AppletEnvironment(final URL URLBASE)
{
    this.URLBASE = URLBASE;
}

public boolean put(String key, String param)
{
    if (PARAMETERS.containsKey(key))
        return false;
    PARAMETERS.put(key, param);
    return true;
}

@Override
public String getParameter(String name) 
{
    return PARAMETERS.get(name);
}

@Override
public URL getCodeBase() 
{
    return URLBASE;
}

@Override
public URL getDocumentBase() 
{
    return URLBASE;
}

@Override
public boolean isActive() 
{
    return true;
}

@Override
public AppletContext getAppletContext()
{
    return null;
}

@Override
public void appletResize(int width, int height) 
{

}
}

Now to make that class work you'll need something like the below. So you can create the "stub" variable.

AppletEnvironment stub = new AppletEnvironment(url);

You'll notice the AppletEnvironment class has a method "put(String key, String param)". This must be done correctly. When you parse the website you'll find there are client parameters which are used to generate the client. If none are found then you can ignore this. If you'd like to see what ones are needed.

Then add the following code in the "getParameter(String name);" method.

System.out.println(name);


来源:https://stackoverflow.com/questions/15467459/loading-a-jar-applet-inside-of-a-jframepanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!