在一个新的JFrame中运行一个Applet(在JAR中)



我想在新的JFrame中运行minecraft.jar中的MinecraftApplet.class。

然而,我正在询问如何做到这一点,因为我现有的代码不起作用:

这是StartScript,它加载jar URL,然后更新类路径,最后在GameFrame中启动一个线程并添加createApplet。

public class StartScript implements Runnable{
public ClassLoader classLoader;
public boolean natives_loaded;
protected URL[] urlList;
public void run() 
{
    try
    {
        String path = (String)AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws Exception {
                return InfiniLauncherUtils.getWorkingDirectory() + File.separator + "bin" + File.separator;
        }});
        File dir = new File(path);
        if (!dir.exists()) 
        {
            dir.mkdirs();
        }
        loadJarURLs();
        updateClassPath(dir);
        Thread start = new Thread(new InfiniLauncherGameFrame(createApplet()));
        start.start();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }    
}
public Applet createApplet() throws InstantiationException, IllegalAccessException, ClassNotFoundException
{
    Class appletClass = classLoader.loadClass("net.minecraft.client.MinecraftApplet");
    return (Applet)appletClass.newInstance();
}

protected void updateClassPath(File dir)throws Exception
{
    URL[] urls = new URL[this.urlList.length];
    for (int i = 0; i < this.urlList.length; i++) {
        urls[i] = new File(dir, getJarName(this.urlList[i])).toURI().toURL();
        System.out.println(urlList[i]);
    }
    if (classLoader == null) {
        classLoader = new URLClassLoader(urls) {
            protected PermissionCollection getPermissions(CodeSource codesource) 
            {
                PermissionCollection perms = null;
                try
                {
                    Method method = SecureClassLoader.class.getDeclaredMethod("getPermissions", new Class[] { CodeSource.class });
                    method.setAccessible(true);
                    perms = (PermissionCollection)method.invoke(getClass().getClassLoader(), new Object[] { 
                      codesource });
                    String host = "www.minecraft.net";
                    if ((host != null) && (host.length() > 0))
                    {
                      perms.add(new SocketPermission(host, "connect,accept"));
                    } else codesource.getLocation().getProtocol().equals("file");
                    perms.add(new FilePermission("<<ALL FILES>>", "read"));
                }
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
                    return perms;
                }
        };
    }
    String path = dir.getAbsolutePath();
    if (!path.endsWith(File.separator)) path = path + File.separator;
    unloadNatives(path);
    System.setProperty("org.lwjgl.librarypath", path + "natives");
    System.setProperty("net.java.games.input.librarypath", path + "natives");
    natives_loaded = true;
}
protected void loadJarURLs() throws Exception {
    String jarList[] = {"lwjgl.jar", "jinput.jar", "lwjgl_util.jar",  LoginScript.mainGameURL};
    //StringTokenizer jar = new StringTokenizer(jarList, ", ");
    int jarCount = jarList.length+1;
    this.urlList = new URL[jarCount];
    URL path = new URL("http://s3.amazonaws.com/MinecraftDownload/");
    for (int i = 0; i < jarCount - 1; i++) {
      this.urlList[i] = new URL(path, jarList[i]);
      System.out.println(urlList[i]);
    }
    String osName = System.getProperty("os.name");
    String nativeJar = null;
    if (osName.startsWith("Win"))
      nativeJar = "windows_natives.jar.lzma";
    else if (osName.startsWith("Linux"))
      nativeJar = "linux_natives.jar.lzma";
    else if (osName.startsWith("Mac"))
      nativeJar = "macosx_natives.jar.lzma";
    else if ((osName.startsWith("Solaris")) || (osName.startsWith("SunOS")))
      nativeJar = "solaris_natives.jar.lzma";
    else {
    }
    if (nativeJar == null) {
    } else {
      this.urlList[(jarCount - 1)] = new URL(path, nativeJar);
    }
  }

protected String getJarName(URL url)
{
    System.out.println(url);
    String fileName = url.toString();
    if (fileName.contains("?")) 
    {
        fileName = fileName.substring(0, fileName.indexOf("?"));
    }
    if (fileName.endsWith(".pack.lzma"))
    {
        fileName = fileName.replaceAll(".pack.lzma", "");
    }
    else if (fileName.endsWith(".pack"))
    {
        fileName = fileName.replaceAll(".pack", "");
    }
    else if (fileName.endsWith(".lzma")) 
    {
        fileName = fileName.replaceAll(".lzma", "");
    }
    return fileName.substring(fileName.lastIndexOf('/') + 1);
  }
private void unloadNatives(String nativePath)
{
    if (!natives_loaded) {
        return;
    }
    try
    {
        Field field = ClassLoader.class.getDeclaredField("loadedLibraryNames");
        field.setAccessible(true);
        Vector libs = (Vector)field.get(getClass().getClassLoader());
        String path = new File(nativePath).getCanonicalPath();
        for (int i = 0; i < libs.size(); i++) {
            String s = (String)libs.get(i);
            if (s.startsWith(path)) {
                libs.remove(i);
                i--;
            }
        }
    } 
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

}

GameFrame类

public InfiniLauncherGameFrame(Applet aapplet)
{
    applet = aapplet;
    System.out.println("ran");
}

public void run() 
{
    System.out.println("few");
    removeAll();
    add(applet);
    validate();
    setVisible(true);
    setSize(300, 300);
}

当我运行它时,我没有得到任何错误,但是新的框架是黑色的,左上角有一个白色的小框。

此处图片:https://i.stack.imgur.com/QI3uI.jpg

我可能不会出错,因为我去掉了几个例外。

非常感谢您的帮助。

也许这是一个愚蠢的建议,但可能是因为您没有显式调用repaint()方法吗?这种情况经常发生在我身上