如何在java中将Applet添加到JPanel [英] how to add a Applet to a JPanel in java
问题描述
我正在做一个项目,但我偶然发现了一个问题,我似乎无法将 Applet 添加到 JPanel.人们一直告诉我它是一个组件,我可以将它添加到 JPanel 中,但它不显示给我.我在这方面遇到了很多麻烦,不胜感激 :)
Hi i am working on a project and i stumbled along a problem where i cannot seem to add a Applet to a JPanel. People kept telling me that it is a component and i can just add it to the JPanel but it isnt displaying for me. I am having a lot of trouble with this and any help is appreciated :).
如果你想要这里的代码(这段代码是例子,不是我的项目):
If you want here is the code (this code is example not my project):
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String artgs[]) throws Exception {
String source = getPageSource(new URL(
"http://www.runescape.com/game.ws?j=1"));
Matcher matcher = SOURCE_PATTERN.matcher(source);
if (matcher.find()) {
String first = matcher.group(1);
String frameSource = getPageSource(new URL(first));
matcher = ARCHIVE_PATTERN.matcher(frameSource);
Matcher codeMatcher = CODE_PATTERN.matcher(frameSource);
if (matcher.find() && codeMatcher.find()) {
RSStub stub = new RSStub(PARAMETER_PATTERN, frameSource);
URL world = new URL(first.substring(0, first.indexOf("/,")));
stub.setCodeBase(world);
stub.setDocumentBase(world);
String archive = matcher.group(1);
System.out.println(world.toString() + "/" + archive);
Download(world.toString() + "/", archive);
URLClassLoader clazzes = new URLClassLoader(
new URL[] { new URL("" + world.toString() + "/"
+ archive) });
Class<?> clazz = clazzes.loadClass("Rs2Applet");
final Applet applet = (Applet) clazz.newInstance();
final JFrame frame = new JFrame();
applet.setStub(stub);
applet.init();
applet.start();
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
applet.stop();
applet.destroy();
frame.dispose();
frame.setVisible(false);
}
});
JPanel panel = new JPanel();
panel.setBackground(Color.black);
panel.setPreferredSize(new Dimension());
panel.add(applet);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
}
private static String getPageSource(URL url) throws IOException,
InterruptedException {
URLConnection c = url.openConnection();
c.addRequestProperty(
"Accept",
"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); // /By
// wyn10
c.addRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
c.addRequestProperty("Accept-Encoding", "gzip,deflate");
c.addRequestProperty("Accept-Language", "en-gb,en;q=0.5");
c.addRequestProperty("Connection", "keep-alive");
c.addRequestProperty("Host", "www.runescape.com");
c.addRequestProperty("Keep-Alive", "300");
c.addRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
DataInputStream di = new DataInputStream(c.getInputStream());
byte[] tmp = new byte[c.getContentLength()];
di.readFully(tmp);
di.close();
return new String(tmp);
}
public static void Download(String world, String archive) throws Exception {
URLConnection jarConnection = new URL(world + archive).openConnection();
FileOutputStream out = new FileOutputStream("./gamepack.jar");
InputStream input = jarConnection.getInputStream();
byte[] tmp = new byte[1024];
int read;
while ((read = input.read(tmp)) != -1) {
out.write(tmp, 0, read);
}
}
public static final Pattern SOURCE_PATTERN = Pattern
.compile("src=\"(.*)\" ");
public static final Pattern ARCHIVE_PATTERN = Pattern
.compile("archive=(.*) ");
public static final Pattern CODE_PATTERN = Pattern.compile("code=(.*) ");
public static final Pattern PARAMETER_PATTERN = Pattern
.compile("<param name=\"([^\\s]+)\"\\s+value=\"([^>]*)\">");
}
存根
import java.applet.AppletContext;
import java.applet.AppletStub;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RSStub implements AppletStub {
private URL documentBase;
private URL codeBase;
public void setDocumentBase(URL documentBase) {
this.documentBase = documentBase;
}
public void setCodeBase(URL codeBase) {
this.codeBase = codeBase;
}
public String[] keys = new String[2];
private Map<String, String> parameters = new HashMap<String, String>();
public RSStub(Pattern parameterPattern, String frameSource) {
Matcher param = parameterPattern.matcher(frameSource);
while (param.find()) {
String key = param.group(1);
String value = param.group(2);
parameters.put(key, value);
if (key.equals("0") || key.equals("-1")) {
switch (key) {
case "0":
keys[0] = key;
case "-1":
keys[1] = key;
}
System.out.println("\t-> Client Deobfucation Key " + key
+ " is " + value);
}
}
}
public String[] getDeobfucationKeys() {
return keys;
}
public URL getDocumentBase() {
return documentBase;
}
public URL getCodeBase() {
return codeBase;
}
public String getParameter(String name) {
return parameters.get(name);
}
public AppletContext getAppletContext() {
return null;
}
public void appletResize(int width, int height) {
}
@Override
public boolean isActive() {
return true;
}
}
推荐答案
像 JFrame
和 JDialog
一样,JApplet
是一个 Swing 顶级容器.您或许可以利用此处引用的示例中所示的混合方法.
Like JFrame
and JDialog
, JApplet
is a Swing top-level container. You may be able to leverage the hybrid approach shown in the examples cited here.
这篇关于如何在java中将Applet添加到JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!