13.3 Making windows and applets with the Java 1.1 AWT



Often you'll want to be able to create a class that can be invoked as either a window or an applet. To accomplish this, you simply add a main( ) to your applet that builds an instance of the applet inside a Frame. As a simple example, let's look at Button2New.java modified to work as both an application and an applet:

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:

  1. Make the handle applet a static member of the class (instead of a local variable of main( )), and then:
  2. Call applet.stop( ) and applet.destroy( ) inside WindowAdapter.windowClosing( ) before you call System.exit( ).
Notice the last line: 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:

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.

Making the window listener an anonymous class

Any of the listener classes could be implemented as anonymous classes, but there's always a chance that you might want to use their functionality elsewhere. However, the window listener is used here only to close the application's window so you can safely make it an anonymous class. Then, in main( ), the line: will become: 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.

Packaging the applet into a JAR file

An important JAR use is to optimize applet loading. In Java 1.0, people tended to try to cram all their code into a single Applet class so the client would need only a single server hit to download the applet code. Not only did this result in messy, hard to read (and maintain) programs, but the .class file was still uncompressed so downloading wasn't as fast as it could have been.

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:

This 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
JAR file, like this: Everything else about applet tags in HTML files remains the same.


Next Page