问题描述
你好我工作的一个项目,我沿着一个问题,我似乎在哪里可以不是一个小程序添加到JPanel跌跌撞撞。人们不停地告诉我,这是一个组件,我可以将它添加到JPanel的,但它不是显示我。我有很多的麻烦与此任何帮助AP preciated。)
如果你想在这里是code(这code是例子不是我的项目):
进口java.applet.Applet中;
进口java.awt.Color中;
进口java.awt.Dimension中;
进口java.awt.event.WindowAdapter中;
进口java.awt.event.WindowEvent中;
进口java.io.DataInputStream中;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口的java.net.URL;
进口java.net.URLClassLoader的;
进口java.net.URLConnection中;
进口java.util.regex.Matcher中;
进口的java.util.regex.Pattern;进口javax.swing.JFrame中;
进口javax.swing.JPanel中;公共类主要{ 公共静态无效的主要(字符串artgs [])抛出异常{
字符串源= getPageSource(新URL(
http://www.runescape.com/game.ws?j=1));
匹配匹配= SOURCE_PATTERN.matcher(源);
如果(matcher.find()){
串第一= matcher.group(1);
字符串frameSource = getPageSource(新URL(第一));
匹配= ARCHIVE_PATTERN.matcher(frameSource);
匹配codeMatcher = code_PATTERN.matcher(frameSource);
如果(matcher.find()及和放大器; codeMatcher.find()){
RSStub存根=新RSStub(PARAMETER_PATTERN,frameSource);
URL =世界新的URL(first.substring(0,first.indexOf(/)));
stub.set codeBase的(世界);
stub.setDocumentBase(世界);
串归档= matcher.group(1);
的System.out.println(world.toString()+/+存档);
下载(world.toString()+/,存档);
URLClassLoader的clazzes =新的URLClassLoader(
新的URL [] {新的URL(+ world.toString()+/
+存档)});
类<> clazz所= clazzes.loadClass(Rs2Applet);
最后Applet小程序=(小程序)clazz.newInstance();
最终的JFrame帧=新的JFrame(); applet.setStub(存根);
applet.init();
applet.start();
frame.addWindowListener(新WindowAdapter的(){
@覆盖
公共无效的windowClosing(WindowEvent五){
applet.stop();
applet.destroy();
frame.dispose();
frame.setVisible(假);
}
});
的JPanel面板=新JPanel();
panel.setBackground(Color.black);
panel.set preferredSize(新尺寸());
panel.add(小程序);
frame.add(面板);
frame.pack();
frame.setVisible(真);
}
}
} 私有静态字符串getPageSource(网址URL)抛出IOException异常,
InterruptedException的{
URLConnection的C = url.openConnection();
c.addRequestProperty(
接受,
\"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\"); // /通过
// wyn10
c.addRequestProperty(接收字符集,ISO-8859-1,utf-8的; Q = 0.7,*; Q = 0.7);
c.addRequestProperty(接受编码,gzip的,放气);
c.addRequestProperty(接受语言,EN-GB,连接; Q = 0.5);
c.addRequestProperty(连接,保持活动);
c.addRequestProperty(主机,www.runescape.com);
c.addRequestProperty(保持活动,300);
c.addRequestProperty(用户代理,
Mozilla的/ 5.0(Windows NT的6.1; RV:9.0.1)的Gecko / 20100101火狐/ 9.0.1);
DataInputStream以DI =新DataInputStream所(c.getInputStream());
字节[] TMP =新的字节[c.getContentLength()];
di.readFully(TMP);
di.close();
返回新的String(TMP);
} 公共静态无效下载(世界字符串,字符串归档)抛出异常{
URLConnection的jarConnection =新的URL(世界+存档).openConnection();
FileOutputStream中出=新的FileOutputStream(./ gamepack.jar);
输入的InputStream = jarConnection.getInputStream();
字节[] TMP =新的字节[1024];
INT读;
而((读= input.read(TMP))!= -1){
out.write(TMP,0,读);
}
} 公共静态最终模式SOURCE_PATTERN =模式
.compile(SRC = \\(*)\\); 公共静态最终模式ARCHIVE_PATTERN =模式
.compile(档案=(*)。); 公共静态最后的模式code_PATTERN = Pattern.compile(code =(*)。); 公共静态最终模式PARAMETER_PATTERN =模式
.compile(&下; PARAM NAME = \\([^ \\\\秒] +)\\\\\\ S +值= \\([^]的计算值*)\\>中);
}
存根
进口java.applet.AppletContext中;
进口java.applet.AppletStub中;
进口的java.net.URL;
进口的java.util.HashMap;
进口的java.util.Map;
进口java.util.regex.Matcher中;
进口的java.util.regex.Pattern;公共类RSStub实现AppletStub { 私人网址文档基; 私人网址codeBase的; 公共无效setDocumentBase(URL文档基){
this.documentBase =文档基;
} 公共无效设置codeBase的(URL codeBase类){
这codeBase的= codeBase的。
} 公众的String []键=新的String [2]; 私人地图<字符串,字符串>参数=新的HashMap<字符串,字符串>(); 公共RSStub(模式parameterPattern,弦乐frameSource){ 匹配参数= parameterPattern.matcher(frameSource);
而(param.find()){
字符串键= param.group(1);
字符串值= param.group(2);
parameters.put(键,值);
如果(key.equals(0)|| key.equals( - 1)){
开关(键){
情况下为0:
键[0] =键;
情况1:
键[1] =键;
}
的System.out.println(\\&T- GT;客户Deobfucation键+键
+就是+值);
}
}
} 公众的String [] getDeobfucationKeys(){
返回键;
} 公共URL getDocumentBase(){
返回文档基;
} 公共网址得到codeBase的(){
返回codeBase的;
} 公共字符串的getParameter(字符串名称){
返回parameters.get(名);
} 公共AppletContext getAppletContext(){
返回null;
} 公共无效appletResize(INT宽度,高度INT){
} @覆盖
公共布尔isActive(){
返回true;
}
}
如同的JFrame
和的JDialog
, JApplet的
是一个Swing 。您可以利用示例中显示引的混合方式。
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=\"([^>]*)\">");
}
Stub
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. You may be able to leverage the hybrid approach shown in the examples cited here.
这篇关于如何将一个小程序在java中添加一个JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!