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

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

问题描述

我正在尝试为我最喜欢的 Java 游戏之一制作 JFrameable加载器",但我不知道如何将实际的 .Jar 加载到 JFrame 面板中(有人告诉我这被称为小程序,我猜我有点落后)

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)

我在 JFrame 中设置了面板和我想要的所有东西,但我不知道如何加载 .jar 并向其发送参数并告诉它我想要的位置在.

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.

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

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

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

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

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

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

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

您会注意到 AppletEnvironment 类有一个方法put(String key, String param)".这必须正确完成.当您解析网站时,您会发现有用于生成客户端的客户端参数.如果没有找到,那么你可以忽略它.如果你想看看需要什么.

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.

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

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

System.out.println(name);

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

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