📄 tij316.htm
字号:
<a name="_Toc24775868"></a><a name="Heading18567"></a>Using
<i>Appletviewer</i></h3>
<p>Sun’s JDK contains a tool called the <a name="Index1680"></a><i>Appletviewer</i> that picks the <b><applet></b> tags out of the HTML file and runs the applets without displaying the surrounding HTML text. Because the Appletviewer ignores everything but APPLET tags, you can put those tags in the Java source file as comments:<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>// <applet code=MyApplet width=200 height=100></applet></font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>This way, you can run “<b>appletviewer MyApplet.java</b>” and you don’t need to create tiny HTML files to run tests. For example, you can add the commented HTML tags to <b>Applet1.java</b>:<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c14:Applet1b.java</font>
<font color=#009900>// Embedding the applet tag for Appletviewer.</font>
<font color=#009900>// <applet code=Applet1b width=100 height=50></applet></font>
<font color=#0000ff>import</font> javax.swing.*;
<font color=#0000ff>import</font> java.awt.*;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Applet1b <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>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Now you can invoke the applet with the command<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>appletviewer Applet1b.java</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>In this book, this form will be used for easy testing of applets. Shortly, you’ll see another coding approach that will allow you to execute applets from the command line without the Appletviewer. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1932" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc24775869"></a><a name="Heading18588"></a>Testing applets</h3>
<p>You can perform a simple test without any network connection by starting up your Web browser and opening the HTML file containing the applet tag. As the HTML file is loaded, the browser will discover the applet tag and go hunt for the <b>.class</b> file specified by the <b>code</b> value. Of course, it looks at the CLASSPATH to find out where to hunt, and if your <b>.class</b> file isn’t in the CLASSPATH, then it will give an error message on the status line of the browser to the effect that it couldn’t find that <b>.class</b> file. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1933" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>When you want to try this out on your Web site, things are a little more complicated. First of all, you must <i>have</i> a Web site, which for most people means a third-party Internet Service Provider (ISP) at a remote location. Since the applet is just a file or set of files, the ISP does not have to provide any special support for Java. You must also have a way to move the HTML files and the <a name="Index1681"></a><a name="Index1682"></a><b>.class</b> files from your site to the correct directory on the ISP machine. This is typically done with a File Transfer Protocol (FTP) program, of which there are many different types available for free or as shareware. So it would seem that all you need to do is move the files to the ISP machine with FTP, then connect to the site and HTML file using your browser; if the applet comes up and works, then everything checks out, right? <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1934" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index1683"></a><a name="Index1684"></a>Here’s where you can get fooled. If the browser on the client machine cannot locate the <b>.class</b> file on the server, it will hunt through the <a name="Index1685"></a><a name="Index1686"></a>CLASSPATH on your <i>local</i> machine. Thus, the applet might not be loading properly from the server, but to you it looks fine during your testing process because the browser finds it on your machine. When someone else connects, however, his or her browser can’t find it. So when you’re testing, make sure you erase the relevant <b>.class</b> files (or <b>.jar </b>file) on your local machine to verify that they exist in the proper location on the server. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1935" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>One of the most insidious places where this happened to me is when I innocently placed an applet inside a <b>package</b>. After uploading the HTML file and applet, it turned out that the 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’s important to specify the full class name including the package in the CODE parameter of your applet tag. In many published applet examples, the applet is not put inside a package, but it’s generally best to use packages in production code. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1936" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc24775870"></a><a name="Heading18593"></a>Running applets from the
command line<br></h2>
<p><a name="Index1687"></a><a name="Index1688"></a>There are times when you’d like to make a windowed program do something else other than sit on a Web page. Perhaps you’d also like it to do some of the things a “regular” application can do, but still have the vaunted instant portability provided by Java. In previous chapters in this book we’ve made command-line applications, but in some operating environments (the Macintosh, for example) there isn’t a command line. So for any number of reasons, you’d like to build a windowed, non-applet program using Java. This is certainly a reasonable desire. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1937" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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<sup><a name="fnB78" href="#fn78">[78]</a></sup> only if you can use the latest version of Java and associated tools so you can deliver applications that won’t confound your users. If for some reason you’re forced to use an older version of Java, think hard before committing to building a significant windowed application. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1938" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index1689"></a><a name="Index1690"></a>Often you’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’re testing the applets, since it’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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1939" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>To create an applet that can be run from the console command line, you simply add a <b>main( )</b> to your applet that builds an instance of the applet inside a <b>Jframe</b>.<sup><a name="fnB79" href="#fn79">[79]</a></sup> As a simple example, let’s look at <b>Applet1b.java</b> modified to work as both an application and an applet:<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c14:Applet1c.java</font>
<font color=#009900>// An application and an applet.</font>
<font color=#009900>// <applet code=Applet1c width=100 height=50></applet></font>
<font color=#0000ff>import</font> javax.swing.*;
<font color=#0000ff>import</font> java.awt.*;
<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>
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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><p><br></p>
<p><b>main( )</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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1940" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>You can see that in <b>main( )</b>, the applet is explicitly initialized and started because 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 <b>stop( )</b> and <b>destroy( )</b>, but for most situations it’s acceptable. If it’s a problem, you can force the calls yourself.<sup><a name="fnB80" href="#fn80">[80]</a></sup> <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1942" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Notice the last line:<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>frame.setVisible(<font color=#0000ff>true</font>);</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Without this, you won’t see anything on the screen. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap13_1943" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc24775871"></a><a name="Heading18631"></a>A display
framework<br></h3>
<p><a name="Index1691"></a><a name="Index1692"></a>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:<br></p>
<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>
run(JFrame frame, <font color=#0000ff>int</font> width, <font color=#0000ff>int</font> height) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setSize(width, height);
frame.setVisible(<font color=#0000ff>true</font>);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -