All of the classes that create a window on the screen (i.e., JWindow, JFrame, JDialog, JInternalFrame and JApplet) have a content pane. This is the main area of the window, and, we saw earlier that a program can access the content pane of an object of one of these classes by executing its getContentPane method.
So, instead of overriding the paint method to output the string "Hello world" as is done by the HelloJApplet applet, we could instead add a JLabel containing this string to the applet's content pane. As we only want to execute the code to add the JLabel object to the content pane once, it is appropriate to put the call of add in an init method of an applet. Here is an applet that does this:
0326: // An applet that adds a JLabel to its content pane. // JLabelJApplet.java 0327: // Barry Cornelius, 3rd May 1999 0328: import java.awt.BorderLayout; 0329: import java.awt.Container; 0330: import javax.swing.JApplet; 0331: import javax.swing.JLabel; 0332: public class JLabelJApplet extends JApplet 0333: { 0334: public void init() 0335: { 0336: final JLabel tJLabel = new JLabel("Hello world"); 0337: final Container tContentPane = getContentPane(); 0338: tContentPane.add(tJLabel, BorderLayout.CENTER); 0339: } 0340: }
Here is a link to a WWW page containing these HTML instructions: http://www.dur.ac.uk/barry.cornelius/papers/advanced+/code2/Applets/simple/JLabelJApplet.html.