JApplet - Implements a swing applet that displays a simple JLabel

Home  »  Java  »  Advance Java »  »  Swing »  JApplet

The Swing set introduces its own version of applet, whose parent is the AWT applet. Swing applets are made more sophisticated so that they can support menu bars and layering of components, and allow painting over the components already laid inside the applet.

With support for menu bars, Swing applets can process menu bar. The layering of components is essentially placing components in mulitple layers overlapping each other. This in turn allows the components to be positioned. Swing applets contain a special component called a layered pane to achieve this feature.

The other feature that allows painting over components inside the applet can be accomplished by using a component called a glass pane.

Swing applet are repesented by the class JApplet, which is stored in the package javax.swing. Swing applet can be created by creating a class the extends JApplet. Just like JFrame, the components which has to be added to the JApplet have to be first added to the content pane. The Swing library introduces this intermediate container to deal with the complexities involved in mixing up the lightweight and the heavyweight components in an applet. The following code statement shows how a component can be added to the content pane inside a Swing applet:

//Get a handle one the applet's content pane



//To assign a new layout



//Add the component to applet



References to the applet's layered pane, glass pane and menu bar can also be obtained by calling the following methods on the applet's instance:



The following example displays a label on a Frame.



The above code will be stored in the file called JAppletTest.java, which on complication will crate a class file called JAppletTest.class.

C:\>jdk1.4\bin>javac JAppletTest.java
C:\>jdk1.4\bin>appletviwer JAppletTest.java

"Output of JAppletTest.class"


Download Complete Program

  -    -  Image




Java Swing-1


JFrame JPanel JApplet
JButtons JLabels JTextFields
JPasswordField JTextAreas JCheckBoxes
JRadioButtons JList JComboBox




No comments:

Post a Comment