public class AppletLiftCycle extends JApplet{ public AppletLiftCycle() throws HeadlessException {
} @Override
public void destroy() {
System.out.println("Applet start() is invoked!");
} @Override
public void init() {
//getContentPane().setLayout(new FlowLayout());
URL code=this.getCodeBase(); //这里报错
System.out.println(code);
getContentPane().add(new JLabel("Applet init() is invoked!"));
System.out.println("Applet init() is invoked!");
} @Override
public void start() {
getContentPane().add(new JLabel("Applet start() is invoked!"));
System.out.println("Applet start() is invoked!");
repaint();
} @Override
public void stop() {
System.out.println("Applet stop() is invoked!");
} public static void main(String[] args) {
JApplet applet = new AppletLiftCycle();//实例化一个applet
JFrame frame = new JFrame("AppletLifeCycle");//实例化一个顶级窗口frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);//将applet放到顶级窗口中
frame.setSize(200, 100);
applet.init();
applet.start();
frame.setVisible(true);
}
}
} @Override
public void destroy() {
System.out.println("Applet start() is invoked!");
} @Override
public void init() {
//getContentPane().setLayout(new FlowLayout());
URL code=this.getCodeBase(); //这里报错
System.out.println(code);
getContentPane().add(new JLabel("Applet init() is invoked!"));
System.out.println("Applet init() is invoked!");
} @Override
public void start() {
getContentPane().add(new JLabel("Applet start() is invoked!"));
System.out.println("Applet start() is invoked!");
repaint();
} @Override
public void stop() {
System.out.println("Applet stop() is invoked!");
} public static void main(String[] args) {
JApplet applet = new AppletLiftCycle();//实例化一个applet
JFrame frame = new JFrame("AppletLifeCycle");//实例化一个顶级窗口frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);//将applet放到顶级窗口中
frame.setSize(200, 100);
applet.init();
applet.start();
frame.setVisible(true);
}
}
public void init() {
// getContentPane().setLayout(new FlowLayout());
getContentPane().add(new JLabel("Applet init() is invoked!"));
System.out.println("Applet init() is invoked!");
URL code = this.getCodeBase(); // 这里报错
System.out.println(code);
}
是 JApplet applet = new AppletLiftCycle();//实例化一个applet ?
还是applet.init(); ?
不过我想不是权限的问题,因为是在应用程序中调用applet所以用getCodeBase()获取不出URL所以才报错
问题就出在这行:
URL code = this.getCodeBase(); // 这里报错
请问有什么解决方法吗 在网上找了好久都没找到 貌似不可以这样写 我用的是MAC 系统
public void init() {
// getContentPane().setLayout(new FlowLayout()); getContentPane().add(new JLabel("Applet init() is invoked!"));
System.out.println("Applet init() is invoked!");
URL code = this.getCodeBase(); // 这里报错
System.out.println(code);
}
访问或修改 null 对象的字段。
将 null 作为一个数组,获得其长度。
将 null 作为一个数组,访问或修改其时间片。
将 null 作为 Throwable 值抛出。
应用程序应该抛出该类的实例,指示其他对 null 对象的非法使用。
当然了,Applet的技术今天已老了,好像过时了,不知道是正常的。1)Applet是嵌在WEB页上在浏览器容器中运行的。
2)或者:Applet是嵌在WEB页上,由AppletViewer容器运行的。
以上两种方式,都是由容器行自动生成AppletStub对象,然后通过Applet的setStub(..)方法传入,Applet的getCodeBase()等都是由该AppletStub对象实际完成的。
3)楼主是由自己的应用程序来运行Applet,实际上:楼主的应用程序就是Applet的容器了,而楼主的应用程序没有遵守容器的规则来写,即:楼主的应用程序没有自动替Applet生成AppletStub的对象,正是AppletStub对象是null,它才是导致那个NullException的真正原因。
怎么办?
很简单:楼主的那个应用程序实现AppletStub接口,使得它成为Applet的一种运行的容器,然后将this(即实现了AppletStub接口的对象通过Applet对象的setStub(..)传入给它就行了)说明:楼主在写什么?实际上:楼主自己重新写了一个自己版本的AppletViewer了。
@Override
public URL getCodeBase() {
throw new UnsupportedOperationException("Not supported yet.");
// setStub(this); 你说的是这样吗 可是编译不通过啊
//这里怎么写才能获取到applet代码的URL?
}
import java.applet.*;
import javax.swing.*;
import java.net.*;
public class AppletLiftCycle extends JApplet implements AppletStub{ public AppletLiftCycle() {
} @Override
public void destroy() {
System.out.println("Applet start() is invoked!");
} @Override
public void init() {
//getContentPane().setLayout(new FlowLayout());
URL code=this.getCodeBase(); //这里报错
System.out.println(code);
getContentPane().add(new JLabel("Applet init() is invoked!"));
System.out.println("Applet init() is invoked!");
} @Override
public void start() {
getContentPane().add(new JLabel("Applet start() is invoked!"));
System.out.println("Applet start() is invoked!");
repaint();
} @Override
public void stop() {
System.out.println("Applet stop() is invoked!");
} public static void main(String[] args) {
JApplet applet = new AppletLiftCycle();//实例化一个applet
applet.setStub((AppletStub)applet);
JFrame frame = new JFrame("AppletLifeCycle");//实例化一个顶级窗口frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);//将applet放到顶级窗口中
frame.setSize(200, 100);
applet.init();
applet.start();
frame.setVisible(true);
} public void appletResize(int width, int height) {
// TODO Auto-generated method stub
}
}