我学习的步骤从赫伯特·希图书Java2的完整参考第五版Java的步骤。 在我去创建简单的横幅Applet小程序的其中显示的横幅和滚动它Applet的浏览器创建线程,并要求重画()方法。 但同时装箱运行的目标的线程对象会抛出这样的例外
java.security.AccessControlException: access denied (java.lang.RuntimePermission modifyThreadGroup)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:345)
at java.security.AccessController.checkPermission(AccessController.java:555)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at sun.applet.AppletSecurity.checkAccess(AppletSecurity.java:252)
at java.lang.ThreadGroup.checkAccess(ThreadGroup.java:304)
at java.lang.ThreadGroup.<init>(ThreadGroup.java:119)
at java.lang.ThreadGroup.<init>(ThreadGroup.java:95)
at Applet.SimpleBanner.start(SimpleBanner.java:49)
at sun.applet.AppletPanel.run(AppletPanel.java:475)
at java.lang.Thread.run(Thread.java:713)
我读的另一篇文章说,它需要安全权限,但在我applet.policy文件已经存在允许的所有权限
grant { permission java.security.AllPermission;};
这只是我的第二小应用程序。 为什么它被抛出安全例外及深入浅出的方案中,详细任何人都可以解释一下吗?
这里是我的applet代码。
import java.applet.Applet;
import java.awt.*;
/* A Simple Banner Applet.
* This Banner Applet Creates a thread that scrolls the message contained
* in msg right to left across banner's window.
*/
/*
* <applet code="SimpleBanner" width=300 height=50>
* </applet>
*/
public class SimpleBanner extends Applet implements Runnable{
private static final long serialVersionUID = 1L;
String msg = "Hello World";
Thread t = null;
ThreadGroup Grp;
int state;
boolean stopflag;
/**
* Initialization method that will be called after the applet is loaded into
* the browser.
*/
@Override
public void init()
{
//Set Foreground and background color
setBackground(Color.cyan);
setForeground(Color.red);
}
@Override
public void start()
{
//Start Thread
Grp = new ThreadGroup("Group");
t = new Thread(Grp, this);
stopflag = false;
t.start();
}
//Entry Point for Thread that Runs The banner
@Override
public void run()
{
char ch;
//Display Banner
for(;;)
{
try {
repaint();
Thread.sleep(250);
ch=msg.charAt(0);
msg =msg.substring(1,msg.length());
msg +=ch;
if (stopflag)
{
break;
}
} catch (InterruptedException ex) {
}
}
}
@Override
public void stop()
{
// Pause The Banner
stopflag=true;
t=null;
}
@Override
public void paint(Graphics g)
{
//Display The Banner
g.drawString(msg, 50, 30);
}
}