- Question: Which classes can an applet extend?
Answer: An applet can extend the
java.applet.Applet
class or thejava.swing.JApplet
class.The
java.applet.Applet
class extends thejava.awt.Panel
class and enables you to use the GUI tools in the AWT package.The
java.swing.JApplet
class is a subclass ofjava.applet.Applet
that also enables you to use the Swing GUI tools.
- Question: How do you cause the applet GUI in the browser to be refreshed when data in it may have changed?
Answer: You invoke the
repaint()
method. This method causes thepaint
method, which is required in any applet, to be invoked again, updating the display.
- Question: For what do you use the
start()
method?Answer: You use the
start()
method when the applet must perform a task after it is initialized, before receiving user input. Thestart()
method either performs the applet's work or (more likely) starts up one or more threads to perform the work.
- Question: True or false: An applet can make network connections to any host on the Internet.
Answer: False: An applet can only connect to the host that it came from.
- Question: How do you get the value of a parameter specified in the
APPLET
tag from within the applet's code?Answer: You use the
getParameter("Parameter name")
method, which returns the String value of the parameter.
- Question: True of false: An applet can run multiple threads.
Answer: True. The
paint
andupdate
methods are always called from the AWT drawing and event handling thread. You can have your applet create additional threads, which is recommended for performing time-consuming tasks.
- Question: Match the following tag names with the descriptions in the following lists:
EMBED
tagAPPLET
tagOBJECT
tag
- Use to deploy applets to a multi-browser environment.
- Use to deploy applets that are to be used only with the Mozilla family of browsers
- Use to deploy applets that are to be used only with Internet Explorer
Answer:
EMBED
tag: bAPPLET
tag: aOBJECT
tag: c
- Exercise: For an applet using the
Exercise
class, write theApplet
tag that sets theButtonName
parameter toSave
.Answer:
<APPLET CODE=AppletButton.class> <PARAM NAME=ButtonName VALUE=Save> </APPLET>
- Exercise: Write the method to display an applet in the browser, so that the contents are contained in a rectangle around the phrase "Exercise Applet". Have the applet be one pixel less than the size specified ont he web page, and the phrase starting at the coordinates 5, 15.
Answer:
public void paint(Graphics g) { //Draw a Rectangle around the applet's display area. g.drawRect(0, 0, getWidth() - 1, getHeight() - 1); //Draw the current string inside the rectangle. g.drawString("Exercise Applet", 5, 15); }