Thursday 14 May 2015

Working with Applications in Java

  Although all the examples in this chapter have been applets (a conscious choice because most of the current excitement about Java is related to its role in the WWW), you can use almost all these techniques in an application. Unless you're using applet-specific methods, you can replace Applet with Panel. So, instead of extending Applet, you extend Panel. You also can still extend the Applet class but put the Applet into a Frame.

Note
An example of an applet-specific method is getImage. In an application, you can use the Toolkit's getImage method instead and everything else stays the same.


Note
Remember that Applet inherits from Component, so you can place an applet in a container-such as a frame-just as you would put a button in the frame.


The general approach is to add a main method to your code, create a Frame, add your class that extends Applet or Panel to the Frame, and then call the init and startup methods for the class that extends Applet. Make sure that the event handler for the Frame handles the WINDOW_DESTROY method properly.  

No comments:

Post a Comment