在JFrame(Panel)内加载.Jar Applet [英] Loading a .Jar Applet inside of a JFrame(Panel)

查看:101
本文介绍了在JFrame(Panel)内加载.Jar Applet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用我最喜欢的Java游戏之一制作JFrameable"Loader",但我不知道如何将实际的.Jar加载到JFrame面板中(有人告诉我这被称为Applet ,猜想我落后了一点)

我已经在面板上设置了JFrame,并在需要的地方进行了设置,但是我不知道如何加载.jar并向其发送参数,并告诉它我想要的位置./p>

任何帮助或链接将不胜感激...因为我找不到任何东西

解决方案

由于您没有提供所使用的站点,因此我仅解释一些部分.

首先,您需要下载游戏.JAR文件或使其可访问.

其次,您需要了解主类,通常称为"main.class".

第三,您需要类似于下面的代码来加载.jar文件主文件.再次将"main.class"变量更改为实际的".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);

您会注意到方法"applet.setStub(stub)".您需要创建一个AppletStub类.与下面的操作类似的操作就足够了.

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) 
{

}
}

现在要使该类正常工作,您将需要以下类似内容.因此,您可以创建"stub"变量.

AppletEnvironment stub = new AppletEnvironment(url);

您会注意到AppletEnvironment类具有方法"put(String key,String param)".这必须正确完成.解析网站时,您会发现有用于生成客户端的客户端参数.如果未找到,则可以忽略此.如果您想知道需要什么.

然后在"getParameter(String name);"中添加以下代码方法.

System.out.println(name);

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

解决方案

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

这篇关于在JFrame(Panel)内加载.Jar Applet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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