PDA

View Full Version : [Java] JPanel to JApplet



TreeFinger
April 13th, 2008, 05:31 PM
Another java question from me. What a surprise.

I am working on a program that is suppose to be a Java Applet. Currently I have 3 files that are part of the program and none of them are Applets.

Is there an easy way to make my program into an applet? I utilize a JPanel in the program.. I don't know if that is a problem.

bovus
April 13th, 2008, 05:36 PM
To use an applet, dont use a main function (thats the main difference I've noticed). instread, have your class extend JApplet and implement the functuion 'public void init()'. In there you put all the components needed (buttons, textboxes, ect.)

bovus
April 13th, 2008, 05:43 PM
oh yea, and to display the applet in a web page, use the hml tag <applet>, like:


<applet
code = "MyClass.class"
width = "800"
height = "700"
>
</applet>

TreeFinger
April 13th, 2008, 06:10 PM
I've created an applet before but I am wondering if there is an easy way to convert a program to an applet? I'm using multiple classes and a JPanel to draw some graphic objects..

I guess I'm just going to re-write the program in applet form without utilizing objects..

Zugzwang
April 14th, 2008, 03:19 PM
I guess I'm just going to re-write the program in applet form without utilizing objects..

No, no! Just separate your program logic:

Have one starting class using "void main(String[] args)" and a window class
Additionally, derive an Applet class for applet loading
Both will do nothing but instantiate a derived JPanel (which contains the rest of your application) and add it to the applet or window .


In both cases, you can use objects in the same way.

This way you get two versions of your program: One as classical program and one as applet.