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

📄 chap13.htm

📁 Thinking in Java, 2nd edition
💻 HTM
📖 第 1 页 / 共 5 页
字号:
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I48' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER13_I49>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now any applet can be run from the
console by creating a <B>main(&#160;)</B> containing a line like
this:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Console.run(<font color=#0000ff>new</font> MyClass(), 500, 300);</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">in which the last two arguments are the
display width and height. Here&#8217;s <B>Applet1c.java</B> modified to use
<B>Console</B>:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c13:Applet1d.java</font>
<font color=#009900>// Console runs applets from the command line.</font>
<font color=#009900>// &lt;applet code=Applet1d 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> Applet1d <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=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Console.run(<font color=#0000ff>new</font> Applet1d(), 100, 50);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This allows the elimination of repeated
code while providing the greatest flexibility in running the examples.

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

<backtalk:display ID=TIJ3_CHAPTER13_I50>
</FONT><A NAME="_Toc481064791"></A><BR></P></DIV>
<A NAME="Heading424"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Using the Windows Explorer</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you&#8217;re using Windows, you can
simplify the process of running a command-line Java program by configuring the
<A NAME="Index1601"></A><A NAME="Index1602"></A>Windows Explorer&#8212;the file
browser in Windows, <I>not</I> the Internet Explorer&#8212;so that you can
simply double-click on a <B>.class</B> file to execute it. There are several
steps in this process. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I50' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER13_I51>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">First, download and install the
<A NAME="Index1603"></A>Perl programming language from <I>www.Perl.org</I>.
You&#8217;ll find the instructions and language documentation on that site.

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

<backtalk:display ID=TIJ3_CHAPTER13_I52>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Next, create the following script without
the first and last lines (this script is part of this book&#8217;s source-code
package):</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//:! c13:RunJava.bat</font>
@rem = '--*-Perl-*--
@echo off
perl -x -S <font color=#004488>"%0"</font> %1 %2 %3 %4 %5 %6 %7 %8 %9
<font color=#0000ff>goto</font> endofperl
@rem ';
#!perl
$file = $ARGV[0];
$file =~ s/(.*)\..*/\1/;
$file =~ s/(.*\\)*(.*)/$+/;
&#180;java $file&#180;;
__END__
:endofperl
<font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now, open the Windows Explorer, select
&#8220;View,&#8221; &#8220;Folder Options,&#8221; then click on the &#8220;File
Types&#8221; tab. Press the &#8220;New Type&#8221; button. For
&#8220;Description of Type&#8221; enter &#8220;Java class file.&#8221; For
&#8220;Associated Extension,&#8221; enter &#8220;class.&#8221; Under
&#8220;Actions&#8221; press the &#8220;New&#8221; button. For
&#8220;Action&#8221; enter &#8220;Open,&#8221; and for &#8220;Application used
to perform action&#8221; enter a line like this:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#004488>"c:\aaa\Perl\RunJava.bat"</font> <font color=#004488>"%L"</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You must customize the path before
&#8220;RunJava.bat&#8221; to conform to the location where you placed the batch
file. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I52' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER13_I53>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Once you perform this installation, you
may run any Java program by simply double-clicking on the <B>.class</B> file
containing a <B>main(&#160;)</B>.

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

<backtalk:display ID=TIJ3_CHAPTER13_I54>
</FONT><A NAME="_Toc375545447"></A><A NAME="_Toc481064792"></A><BR></P></DIV>
<A NAME="Heading425"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Making a button<BR><A NAME="Index1604"></A><A NAME="Index1605"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Making a button is quite simple: you just
call the <B>JButton</B> constructor with the label you want on the button.
You&#8217;ll see later that you can do fancier things, like putting graphic
images on buttons. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I54' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER13_I55>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Usually you&#8217;ll want to create a
field for the button inside your class so that you can refer to it later.

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

<backtalk:display ID=TIJ3_CHAPTER13_I56>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>JButton</B> is a
component&#8212;its own little window&#8212;that will automatically get
repainted as part of an update. This means that you don&#8217;t explicitly paint
a button or any other kind of control; you simply place them on the form and let
them automatically take care of painting themselves. So to place a button on a
form, you do it inside <B>init(&#160;)</B>:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c13:Button1.java</font>
<font color=#009900>// Putting buttons on an applet.</font>
<font color=#009900>// &lt;applet code=Button1 width=200 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> Button1 <font color=#0000ff>extends</font> JApplet {
  JButton 
    b1 = <font color=#0000ff>new</font> JButton(<font color=#004488>"Button 1"</font>), 
    b2 = <font color=#0000ff>new</font> JButton(<font color=#004488>"Button 2"</font>);
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> init() {
    Container cp = getContentPane();
    cp.setLayout(<font color=#0000ff>new</font> FlowLayout());
    cp.add(b1);
    cp.add(b2);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Console.run(<font color=#0000ff>new</font> Button1(), 200, 50);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Something new has been added here: before
any elements are placed on the content pane, it is given a new &#8220;layout
manager,&#8221; of type <B>FlowLayout</B>. The layout manager is the way that
the pane implicitly decides where to place the control on the form. The normal
behavior of an applet is to use the <B>BorderLayout</B>, but that won&#8217;t
work here because (as you will learn later in this chapter when controlling the
layout of a form is examined in more detail) it defaults to covering each
control entirely with every new one that is added. However, <B>FlowLayout</B>
causes the controls to flow evenly onto the form, left to right and top to
bottom.

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

<backtalk:display ID=TIJ3_CHAPTER13_I57>
</FONT><A NAME="_Toc375545448"></A><A NAME="_Toc481064793"></A><BR></P></DIV>
<A NAME="Heading426"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Capturing an event<BR><A NAME="Index1606"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You&#8217;ll notice that if you compile
and run the applet above, nothing happens when you press the buttons. This is
where you must step in and write some code to determine what will happen. The
basis of <A NAME="Index1607"></A><A NAME="Index1608"></A>event-driven
programming, which comprises a lot of what a GUI is about, is tying events to
code that responds to those events.

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

<backtalk:display ID=TIJ3_CHAPTER13_I58>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The way that this is accomplished in
Swing is by cleanly separating the interface (the graphical components) and the
implementation (the code that you want to run when an event happens to a
component). Each Swing component can report all the events that might happen to
it, and it can report each kind of event individually. So if you&#8217;re not
interested in, for example, whether the mouse is being moved over your button,
you don&#8217;t register your interest in that event. It&#8217;s a very
straightforward and elegant way to handle event-driven programming, and once you
understand the basic concepts you can easily use Swing components that you
haven&#8217;t seen before&#8212;in fact, this model extends to anything that can
be classified as a JavaBean (which you&#8217;ll learn about later in the
chapter). 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER13_I58' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER13_I59>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">At first, we will just focus on the main
event of interest for the components being used. In the case of a
<B>JButton</B>, this &#8220;event of interest&#8221; is that the button is
pressed.<B> </B>To register your interest in when a button is pressed, you call
the <B>JButton</B>&#8217;s <B>addActionListener(&#160;)</B> method. This method
expects an argument that is an object that implements the <B>ActionListener</B>
interface, which contains a single method called <B>actionPerformed(&#160;)</B>.
So all you have to do to attach code to a <B>JButton</B> is to implement the
<B>ActionListener</B> interface in a class, and register an object of that class
with the <B>JButton</B> via <B>addActionListener(&#160;)</B>. The method will be
called when the button is pressed (this is normally referred to as a
<A NAME="Index1609"></A><I>callback</I>).

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

<backtalk:display ID=TIJ3_CHAPTER13_I60>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">But what should the result of pressing
that button be? We&#8217;d like to see something change on the screen, so a new
Swing component will be introduced: the
<A NAME="Index1610"></A><B>JTextField</B>. This is a place where text can be
typed, or in this case modified by the program. Although there are a number of
ways to create a <B>JTextField</B>, the simplest is just to tell the constructor
how wide you want that field to be. Once the <B>JTextField</B> is placed on the
form, you can modify its contents by using the <B>setText(&#160;)</B> method
(there are many other methods in <B>JTextField</B>, but you must look these up
in the HTML documentation for the JDK from <I>java.sun.com</I>). Here is what it
looks like:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c13:Button2.java</font>
<font color=#009900>// Responding to button presses.</font>
<font color=#009900>// &lt;applet code=Button2 width=200 height=75&gt;</font>
<font color=#009900>// &lt;/applet&gt;</font>
<font color=#0000ff>import</font> javax.swing.*;
<font color=#0000ff>import</font> java.awt.event.*

⌨️ 快捷键说明

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