0

Guys am trying to close frame within applet... my program is working but the problem is that the frame which will get created within applet is not closing... what should i do... it gives me Exception while closing frame window:

C:\jdk1.6.0\bin>appletviewer me.java                                            
Exception in thread "AWT-EventQueue-1" java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM.0)                                       at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)                                                         at
java.security.AccessController.checkPermission(AccessController.java:546) 

Here is my code:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="me.class" height=500 width=500></applet>*/
class se extends Frame {

    String msg;

    public se() {
        msg="This is Frame";
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public void paint(Graphics g) {
        g.drawString(msg,10,100);
    }
}

public class me extends Applet {

    public void init() {
        se s1=new se();
        s1.setSize(400,400);
        s1.setVisible(true);
        s1.setTitle("The JAVA GAMER");
    }

    public void paint(Graphics g) {
        g.drawString("This is Applet",10,100);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3485153
  • 27
  • 2
  • 5
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Apr 23 '14 at 06:25
  • Answer my questions.. – Andrew Thompson Apr 23 '14 at 12:48

1 Answers1

1
System.exit(0);

This is not permitted by the applet sand-box, even if the applet is fully trusted.

But better to make this a stand-along app. (Frame/JFrame) & deploy it from a link/button on a web page using Java Web Start. JWS works on Windows, OS X & *nix. Then the frame can call System.exit(n) at any security level.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433