📄 13_3.htm
字号:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Weng Kai">
<META NAME="GENERATOR" CONTENT="Mozilla/4.01 [en] (Win95; I) [Netscape]">
<TITLE>Making windows and applets with the Java 1.1 AWT</TITLE>
</HEAD>
<BODY>
<H2>
13.3 Making windows and applets with the Java 1.1 AWT</H2>
<HR WIDTH="100%">
<BR>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:
<P>Case Study: <A HREF="case/Button2NewB.java">Button2NewB.java</A>
<P>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:
<OL>
<LI>
Make the handle applet a static member of the class (instead of a local
variable of main( )), and then:</LI>
<LI>
Call applet.stop( ) and applet.destroy( ) inside WindowAdapter.windowClosing(
) before you call System.exit( ).</LI>
</OL>
Notice the last line:
<UL>
<PRE>aFrame.setVisible(true);</PRE>
</UL>
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.
<P>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.
<P>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:
<UL>
<PRE>aFrame.add(applet, BorderLayout.CENTER);</PRE>
</UL>
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.
<H3>
Making the window listener an anonymous class</H3>
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:
<UL>
<PRE>aFrame.addWindowListener(new WL());</PRE>
</UL>
will become:
<UL>
<PRE>aFrame.addWindowListener(</PRE>
<PRE> new WindowAdapter() {</PRE>
<PRE> public void windowClosing(WindowEvent e) {</PRE>
<PRE> System.exit(0);</PRE>
<PRE> }</PRE>
<PRE> }</PRE>
<PRE>);</PRE>
</UL>
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.
<H3>
Packaging the applet into a JAR file</H3>
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.
<P>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:
<UL>
<PRE>jar cf Button2NewB.jar *.class</PRE>
</UL>
This assumes that the only .class files in the current directory are the
ones from Button2NewB.java (otherwise you抣l get extra baggage). Now you
can create an HTML page with the new archive tag to indicate the name of
the
<BR>JAR file, like this:
<UL>
<PRE><head><title>Button2NewB Example Applet</PRE>
<PRE></title></head></PRE>
<PRE><body></PRE>
<PRE><applet code="Button2NewB.class" archive="Button2NewB.jar" width=200 height=150></PRE>
<PRE></applet></PRE>
<PRE></body></PRE>
</UL>
Everything else about applet tags in HTML files remains the same.
<P>
<HR WIDTH="100%">
<DIV ALIGN=right><A HREF="13_4.htm">Next Page</A></DIV>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -