Case Study: Button2NewB.java
The inner class WL and the main( ) are the only two elements added to the applet, and the rest of the applet is untouched. In fact, you can usually copy and paste the WL class and main( ) into your own applets with little modification. The WL class is static so it can be easily created in main( ). (Remember that an inner class normally needs an outer class handle when it's created. Making it static eliminates this need.) You can see that in main( ), the applet is explicitly initialized and started since in this case the browser isn't available to do it for you. Of course, this doesn't provide the full behavior of the browser, which also calls stop( ) and destroy( ), but for most situations it's acceptable. If it's a problem, you can:
aFrame.setVisible(true);This is one of the changes in the Java 1.1 AWT. The show( ) method is deprecated and setVisible(true) replaces it. These sorts of seemingly capricious changes will make more sense when you learn about Java Beans later in the chapter.
This example is also modified to use a TextField rather than printing to the console or to the browser status line. One restriction in making a program that's both an applet and an application is that you must choose input and output forms that work for both situations.
There's another small new feature of the Java 1.1 AWT shown here. You no longer need to use the error-prone approach of specifying BorderLayout positions using a String. When adding an element to a BorderLayout in Java 1.1, you can say:
aFrame.add(applet, BorderLayout.CENTER);You name the location with one of the BorderLayout constants, which can then be checked at compile-time (rather than just quietly doing the wrong thing, as with the old form). This is a definite improvement, and will be used throughout the rest of the book.
aFrame.addWindowListener(new WL());will become:
aFrame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);This has the advantage that it doesn't require yet another class name. You must decide for yourself whether it makes the code easier to understand or more difficult. However, for the remainder of the book an anonymous inner class will usually be used for the window listener.
JAR files change all of that by compressing all of your .class files into a single file that is downloaded by the browser. Now you don't need to create an ugly design to minimize the number of classes you create, and the user will get a much faster download time. Consider the example above. It looks like Button2NewB is a single class, but in fact it contains three inner classes, so that's four in all. Once you've compiled the program, you package it into a JAR file with the line:
jar cf Button2NewB.jar *.classThis assumes that the only .class files in the current directory are the ones from Button2NewB.java (otherwise you’ll get extra baggage). Now you can create an HTML page with the new archive tag to indicate the name of the
<head><title>Button2NewB Example Applet
</title></head>
<body>
<applet code="Button2NewB.class" archive="Button2NewB.jar" width=200 height=150>
</applet>
</body>Everything else about applet tags in HTML files remains the same.