Java Reference
In-Depth Information
4. The setVisible method should not be called on the JApplet window because it is
always visible.
5. The setTitle and setSize methodsshouldnotbecalledonthe JApplet window
because the title and the size of the applet are determined by the HTML file that
opens the applet.
6. Remove any code that exits the program. An applet does not have a close button.
Similarly, remove any reference to the setDefaultCloseOperation method for the
applet window.
7. An applet can create additional JFrame objects (i.e., windows), but the applet window
itself is not a JFrame object.
Following these principles, consider the following simple Java Applet code.
import java .awt .
;
import javax . swing .
;
public class MyApplet extends JApplet
{
MyPanel p = new MyPanel () ;
public void init() {
p. setS( "Initializing ..." );
p.repaint();
add(p) ;
}
public void start() {
p. setS( "Starting ..." );
p.repaint();
}
public void stop() {
p. setS( "Stopping ..." );
p.repaint();
}
public void destroy()
{
p. setS( "Destroying ..." );
p.repaint();
}
}
class MyPanel extends JPanel
{
String s ;
int i=0;
public void setS(String s)
{
this .s = s;
i ++;
}
public void paintComponent(Graphics g)
{
super . paintComponent(g) ;
g. drawString(s + "" + i , 10, 10) ;
 
Search WWH ::




Custom Search