I have try to run a program of JApplet as JFrame, but it didn't works. Can someone help me?Thanks.

public class MyApplet extends JApplet{

//have some coding here

class display extends JPanel{
//have some coding here
}

public static void main(String [] arg){
	JFrame frame = new JFrame("MyApplet");
	MyApplet applet = new MyApplet();
        applet.init();

	applet.setSize(1000,650);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setVisible(true);
	frame.setResizable(false);
	frame.add(applet,BorderLayout.CENTER);

}

}

Recommended Answers

All 3 Replies

it didn't work

Please explain.

Its best to setVisible after everything has been added to the frame.

To convert an applet to an application, treat the applet's GUI code as a panel (Which applet extends) and add that to the frame. Then add calls to the applet's methods that are called by the browser: init and start.
If the applet uses any AppletContext methods, you will need to provide them to the applet.

Ok, its mean that my sounds clip and images didn't show up when I run it. And I try to add the applet.init() but it turns out to be an error message. Is like below :

Exception in thread "main" java.lang.NullPointerException
at java.applet.Applet.getDocumentBase(Unknown Source)
at MyApplet.init(MyApplet.java:93)
at MyApplet.main(MyApplet.java:161)

If the applet uses any AppletContext/AppletStub methods, you will need to provide them to the applet.
When an applet is executed in a browser, the browser provides an AppletContext and AppletStub objects for the applet to use.

See post#4 on this thread for an example: http://www.java-forums.org/java-applets/30675-writing-hybrid-aplication.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.