An Illustration of these Techniques
3. A Pseudo-Constructor Example
Notice, in this code, that neither applet ‘app’ nor Frame ‘ShopFrame’ contain any variables at all. All
variables are contained in the class ShopPanel. The applet, in its method init(), refers to ShopPanel, which
in and of itself forces all of ShopPanel’s declared variables, all of which here are static, to be instantiated.
In particular, shopFrame and counter are formed. The applet’s life cycle then calls init() in the applet,
which transfers the call to init() in ShopPanel. ShopPanel’s init() calls formPanelElements() in ShopPanel,
which sets the characteristics of the already instantiated objects in ShopPanel. The next call is to
shopFrame, which now exists, and has defined characteristics, and in particular to its method formPanel(),
which takes the frame elements defined in ShopPanel, and sets them into shopFrame, which is also defined
in ShopPanel.
Create the following applet:
import java.applet.*;
public class app extends Applet{
public void init(){
ShopPanel.init();
}
}
import java.awt.*;
import java.awt.event.*;
public class ShopFrame extends Frame implements ActionListener{
void formPanel() {
add(ShopPanel.counter);
ShopPanel.counter.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
ShopPanel.increment();
}
}
import java.awt.*;
//notice that it can be public class ShopPanel,
//not just abstract class ShopPanel. We will discuss this.
public class ShopPanel{
static ShopFrame shopFrame = new ShopFrame();
static Button counter = new Button("Click");
static int r = 0;
static void formPanelElements(){
shopFrame.setBounds(200,200,100,100);
}
static void init(){
formPanelElements();
shopFrame.formPanel();
shopFrame.show();
}
static void increment(){
counter.setLabel(new Integer(r).toString());
r++;
}
}
Place it into the following HTML page:
<HTML>
<HEAD>
</HEAD>
<BODY>
<applet code=app.class name=app width=1
height=1 VIEWASTEXT>
</applet>
</BODY>
</HTML>
Event handling can now be set up, in shopFrame’s formPanel(). Events are caught in
shopFrame, but the event handlers are all located as static methods in ShopPanel. Notice that this structure
is not dependent upon the applet at all – there is a call from ‘app’ to ShopPanel, but nothing from
ShopPanel back to applet ‘app’. The frame shopFrame, which is created indirectly by the applet through
its reference to ShopPanel, in fact knows absolutely nothing about its parent – it is aware only of the
class ShopPanel, which is not being instantiated. When page transfers are made, it is the applet which is
altered from active to inactive – notice how short it is, and how little time will be required to reload it. The
frame doesn’t change at all; it simply sits there, and watches as you browse.
Let’s look now at what our example does. The applet indirectly creates a frame. Clicking this frame
increments a number (there may be problems in some browsers – we are not including minor tweaking
elements, composed solely of standard JAVA, that solve these 'glitches'). Moving away from the HTML
page does not destroy the said frame – rather, it continues to operate as a kind of 'terminate and stay
resident' web program. Notice that the said frame now maintains its position from page to page
automatically, without any user intervention, unlike the previous two examples.
Next ->
Lane Friesen
lanelise@dowco.com
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|