[英]how to add a Applet to a JPanel in java
Hi i am working on a project and i stumbled along a problem where i cannot seem to add a Applet to a JPanel. 嗨,我正在一个项目上,我偶然发现了一个似乎无法向Apple JPanel添加Applet的问题。 People kept telling me that it is a component and i can just add it to the JPanel but it isnt displaying for me. 人们一直告诉我这是一个组件,我可以将其添加到JPanel中,但不会为我显示。 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;
}
}
Like JFrame
and JDialog
, JApplet
is a Swing top-level container . 像JFrame
和JDialog
, JApplet
是一个Swing 顶级容器 。 You may be able to leverage the hybrid approach shown in the examples cited here . 您也许可以利用此处引用的示例中所示的混合方法。
If the desired functionality is adding of an applet to your Panel, then you can do something like this : 如果所需的功能是在面板中添加小程序,则可以执行以下操作:
getContentPane().add();
But first you must extend applet or jApplet. 但是首先您必须扩展applet或jApplet。
This should work . 这应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.