⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap13.htm

📁 Thinking in Java, 2nd edition
💻 HTM
📖 第 1 页 / 共 5 页
字号:
server path to the applet was confused because of the package name. However, my
browser found it in the local CLASSPATH. So I was the only one who could
properly load the applet. It took some time to discover that the <B>package</B>
statement was the culprit. In general, you&#8217;ll want to leave the
<A NAME="Index1588"></A><A NAME="Index1589"></A><B>package</B> statement out of
an applet.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I38' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER13_I39>
</FONT><A NAME="_Toc481064789"></A><BR></P></DIV>
<A NAME="Heading422"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Running applets from the command
line<BR><A NAME="Index1590"></A><A NAME="Index1591"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There are times when you&#8217;d like to
make a windowed program do something else other than sit on a Web page. Perhaps
you&#8217;d also like it to do some of the things a &#8220;regular&#8221;
application can do but still have the vaunted instant portability provided by
Java. In previous chapters in this book we&#8217;ve made command-line
applications, but in some operating environments (the Macintosh, for example)
there isn&#8217;t a command line. So for any number of reasons you&#8217;d like
to build a windowed, non-applet program using Java. This is certainly a
reasonable desire. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I39' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER13_I40>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The Swing library allows you to make an
application that preserves the look and feel of the underlying operating
environment. If you want to build windowed applications, it makes sense to do
so</FONT><A NAME="fnB65" HREF="#fn65">[65]</A><FONT FACE="Georgia"> only if you
can use the latest version of Java and associated tools so you can deliver
applications that won&#8217;t confound your users. If for some reason
you&#8217;re forced to use an older version of Java, think hard before
committing to building a significant windowed application.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I40' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER13_I41>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><A NAME="Index1592"></A><A NAME="Index1593"></A><FONT FACE="Georgia">Often
you&#8217;ll want to be able to create a class that can be invoked as either a
window or an applet. This is especially convenient when you&#8217;re testing the
applets, since it&#8217;s typically much faster and easier to run the resulting
applet-application from the command line than it is to start up a Web browser or
the Appletviewer. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I41' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER13_I42>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To create an applet that can be run from
the console command line, you simply add a <B>main(&#160;)</B> to your applet
that builds an instance of the applet inside a
<B>JFrame</B>.</FONT><A NAME="fnB66" HREF="#fn66">[66]</A><FONT FACE="Georgia">
As a simple example, let&#8217;s look at <B>Applet1b.java</B> modified to work
as both an application and an applet:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c13:Applet1c.java</font>
<font color=#009900>// An application and an applet.</font>
<font color=#009900>// &lt;applet code=Applet1c width=100 height=50&gt;</font>
<font color=#009900>// &lt;/applet&gt;</font>
<font color=#0000ff>import</font> javax.swing.*;
<font color=#0000ff>import</font> java.awt.*;
<font color=#0000ff>import</font> com.bruceeckel.swing.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Applet1c <font color=#0000ff>extends</font> JApplet {
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> init() {
    getContentPane().add(<font color=#0000ff>new</font> JLabel(<font color=#004488>"Applet!"</font>));
  }
  <font color=#009900>// A main() for the application:</font>
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    JApplet applet = <font color=#0000ff>new</font> Applet1c();
    JFrame frame = <font color=#0000ff>new</font> JFrame(<font color=#004488>"Applet1c"</font>);
    <font color=#009900>// To close the application:</font>
    Console.setupClosing(frame);
    frame.getContentPane().add(applet);
    frame.setSize(100,50);
    applet.init();
    applet.start();
    frame.setVisible(<font color=#0000ff>true</font>);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>main(&#160;)</B> is the only element
added to the applet, and the rest of the applet is untouched. The applet is
created and added to a <B>JFrame</B> so that it can be displayed. 

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I42' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER13_I43>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The line:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Console.setupClosing(frame);</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Causes the window to be properly closed.
<B>Console</B> comes from <B>com.bruceeckel.swing</B> and will be explained a
little later. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I43' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER13_I44>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can see that in <B>main(&#160;)</B>,
the applet is explicitly initialized and started since in this case the browser
isn&#8217;t available to do it for you. Of course, this doesn&#8217;t provide
the full behavior of the browser, which also calls <B>stop(&#160;)</B> and
<B>destroy(&#160;)</B>, but for most situations it&#8217;s acceptable. If
it&#8217;s a problem, you can force the calls
yourself.</FONT><A NAME="fnB67" HREF="#fn67">[67]</A><FONT FACE="Georgia">

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I44' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER13_I45>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Notice the last line:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>frame.setVisible(<font color=#0000ff>true</font>);</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Without this, you won&#8217;t see
anything on the screen.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I45' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER13_I46>
</FONT><A NAME="_Toc481064790"></A><BR></P></DIV>
<A NAME="Heading423"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
A display framework<BR><A NAME="Index1594"></A><A NAME="Index1595"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Although the code that turns programs
into both applets and applications produces valuable results, if used everywhere
it becomes distracting and wastes paper. Instead, the following display
framework will be used for the Swing examples in the rest of this
book:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: com:bruceeckel:swing:Console.java</font>
<font color=#009900>// Tool for running Swing demos from the</font>
<font color=#009900>// console, both applets and JFrames.</font>
<font color=#0000ff>package</font> com.bruceeckel.swing;
<font color=#0000ff>import</font> javax.swing.*;
<font color=#0000ff>import</font> java.awt.event.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Console {
  <font color=#009900>// Create a title string from the class name:</font>
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> String title(Object o) {
    String t = o.getClass().toString();
    <font color=#009900>// Remove the word "class":</font>
    <font color=#0000ff>if</font>(t.indexOf(<font color=#004488>"class"</font>) != -1)
      t = t.substring(6);
    <font color=#0000ff>return</font> t;
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> setupClosing(JFrame frame) {
    <font color=#009900>// The JDK 1.2 Solution as an </font>
    <font color=#009900>// anonymous inner class:</font>
    frame.addWindowListener(<font color=#0000ff>new</font> WindowAdapter() {
      <font color=#0000ff>public</font> <font color=#0000ff>void</font> windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    <font color=#009900>// The improved solution in JDK 1.3:</font>
    <font color=#009900>// frame.setDefaultCloseOperation(</font>
    <font color=#009900>//     EXIT_ON_CLOSE);</font>
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> 
  run(JFrame frame, <font color=#0000ff>int</font> width, <font color=#0000ff>int</font> height) {
    setupClosing(frame);
    frame.setSize(width, height);
    frame.setVisible(<font color=#0000ff>true</font>);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> 
  run(JApplet applet, <font color=#0000ff>int</font> width, <font color=#0000ff>int</font> height) {
    JFrame frame = <font color=#0000ff>new</font> JFrame(title(applet));
    setupClosing(frame);
    frame.getContentPane().add(applet);
    frame.setSize(width, height);
    applet.init();
    applet.start();
    frame.setVisible(<font color=#0000ff>true</font>);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> 
  run(JPanel panel, <font color=#0000ff>int</font> width, <font color=#0000ff>int</font> height) {
    JFrame frame = <font color=#0000ff>new</font> JFrame(title(panel));
    setupClosing(frame);
    frame.getContentPane().add(panel);
    frame.setSize(width, height);
    frame.setVisible(<font color=#0000ff>true</font>);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This is a tool you may want to use
yourself, so it&#8217;s placed in the library
<A NAME="Index1596"></A><B>com.bruceeckel.swing</B>. The <B>Console</B> class
consists entirely of <B>static</B> methods. The first is used to extract the
class name (using RTTI) from any object and to remove the word
&#8220;class,&#8221; which is typically prepended by <B>getClass(&#160;)</B>.
This uses the <B>String</B> methods <B>indexOf(&#160;)</B> to determine whether
the word &#8220;class&#8221; is there, and <B>substring(&#160;)</B> to produce
the new string without &#8220;class&#8221; or the trailing space. This name is
used to label the window that is displayed by the <B>run(&#160;)</B> methods.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I46' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER13_I47>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>setupClosing(&#160;)</B> is used to
hide the code that causes a <B>JFrame</B> to exit a program when that
<B>JFrame</B> is closed. The default behavior is to do nothing, so if you
don&#8217;t call <B>setupClosing(&#160;)</B> or write the equivalent code for
your <A NAME="Index1597"></A><B>JFrame</B>, the application won&#8217;t close.
The reason this code is hidden rather than placing it directly in the subsequent
<B>run(&#160;)</B> methods is partly because it allows you to use the method by
itself when what you want to do is more complicated than what <B>run(&#160;)</B>
provides. However, it also isolates a change factor: Java 2 has two ways of
causing certain types of windows to close. In JDK 1.2, the solution is to create
a new <A NAME="Index1598"></A><B>WindowAdapter</B> class and implement
<A NAME="Index1599"></A><B>windowClosing(&#160;)</B>, as seen above (the meaning
of this will be fully explained later in this chapter). However, during the
creation of JDK 1.3 the library designers observed that you typically need to
close windows whenever you&#8217;re creating a non-applet, and so they added the
<A NAME="Index1600"></A><B>setDefaultCloseOperation(&#160;)</B> to <B>JFrame</B>
and <B>JDialog</B>. From the standpoint of writing code, the new method is much
nicer to use but this book was written while there was still no JDK 1.3
implemented on Linux and other platforms, so in the interest of cross-version
portability the change was isolated inside <B>setupClosing(&#160;)</B>.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I47' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER13_I48>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>run(&#160;)</B> method is
overloaded to work with <B>JApplet</B>s, <B>JPanel</B>s, and <B>JFrame</B>s.
Note that only if it&#8217;s a <B>JApplet</B> are <B>init(&#160;)</B> and
<B>start(&#160;)</B> called.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -