All of my tutorials thus far have been using JFrame to create windows. However, Java also supports the Applet, this enables your applications to be viewable in your internet browser. All swing components that have been discussed in my previous tutorials can be used in an Applet. Lets take a look at how an applet with a JButton might look.
We are going to start with our basic class:
When creating the basic class, one of the main differences between a java application and an java applet is that applets do not use the [highlight="Java"]public static void main(String args[])[/highlight] as its main method for instantiation - it uses the init method.Code:package tutorials; public class ApplTut { public ApplTut() { } }
First we must extend the JApplet, just as we extend JFrame when making applications. Also we import javax.swing.JApplet.
Next we add the init method.Code:package tutorials; import javax.swing.JApplet; public class ApplTut extends JApplet { public ApplTut() { } }
And that is the basic shell of an applet. To add swing components, use the same method's you would for an application. Here is an example of how you would add a JButton.Code:package tutorials; import javax.swing.JApplet; public class ApplTut extends JApplet { public ApplTut() { } public void init() { } }
Originally posted as The JAppletCode:package tutorials; import javax.swing.JApplet; import javax.swing.JButton; public class ApplTut extends JApplet { private JButton button; public ApplTut() { } public void init() { button = new JButton("Click Me"); add(button); } }
Last edited by John; 08-01-2010 at 09:29 AM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks