📄 tij0150.html
字号:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>actionListener</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
field is not
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
its
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>actionPerformed( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is called, creating a new <A NAME="Index2245"></A><A NAME="Index2246"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ActionEvent
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">object
in the process. Whenever the mouse is moved, its new coordinates are captured
and the canvas is repainted (erasing any text that’s on the canvas, as
you’ll see).
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is added to allow you to test the program from the command line. When a Bean is
in a development environment,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
will not be used, but it’s helpful to have a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in each of your Beans because it provides for rapid testing.
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">creates
a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Frame</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and places a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
within it, attaching a simple
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ActionListener</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to print to the console whenever an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ActionEvent</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
occurs. Usually, of course, the application builder tool would create most of
the code that uses the Bean.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
you run the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
through
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BeanDumper</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or put the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
inside a Bean-enabled development environment, you’ll notice that there
are many more properties and actions than are evident from the above code.
That’s because
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is inherited from
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Canvas</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Canvas</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is a Bean, so you’re seeing its properties and events as well.
</FONT><a name="_Toc408018721"></a><P></DIV>
<A NAME="Heading458"></A><H3 ALIGN=LEFT>
Packaging
a Bean
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Before
you can bring a Bean into a Bean-enabled visual builder tool, it must be put
into the standard Bean container, which is a <A NAME="Index2247"></A><A NAME="Index2248"></A>JAR
(Java ARchive) file that includes all the Bean classes as well as a
“manifest” file that says “This is a Bean.” A manifest
file is simply a text file that follows a particular form. For the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
the manifest file looks like this:
</FONT><P></DIV>
<font color="#990000"><PRE>Manifest-Version: 1.0
Name: bangbean/BangBean.<font color="#0000ff">class</font>
Java-Bean: True </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
first line indicates the version of the manifest scheme, which until further
notice from Sun is 1.0. The second line (empty lines are ignored) names the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
file, and the third says, “It’s a Bean.” Without the third
line, the program builder tool will not recognize the class as a Bean.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
only tricky part is that you must make sure that you get the proper path in the
“Name:” field. If you look back at
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you’ll see it’s in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package
bangbean
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">(and
thus in a subdirectory called “bangbean” that’s off of the
classpath), and the name in the manifest file must include this package
information. In addition, you must place the manifest file in the directory
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>above</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
the root of your package path, which in this case means placing the file in the
directory above the “bangbean” subdirectory. Then you must invoke
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>jar</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
from the same directory as the <A NAME="Index2249"></A><A NAME="Index2250"></A>manifest
file, as follows:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">jar
cfm BangBean.jar BangBean.mf bangbean
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
assumes that you want the resulting JAR file to be named
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean.jar</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and that you’ve put the manifest in a file called
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean.mf</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
might wonder “What about all the other classes that were generated when I
compiled
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">?”
Well, they all ended up inside the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>bangbean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
subdirectory, and you’ll see that the last argument for the above
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>jar</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
command line is the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>bangbean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
subdirectory. When you give
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>jar</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
the name of a subdirectory, it packages that entire subdirectory into the jar
file (including, in this case, the original
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
source-code file – you might not choose to include the source with your
own Beans). In addition, if you turn around and unpack the JAR file
you’ve just created, you’ll discover that your manifest file
isn’t inside, but that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>jar</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
has created its own manifest file (based partly on yours) called
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MANIFEST.MF
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">and
placed it inside the subdirectory
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>META-INF</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(for “meta-information”). If you open this manifest file
you’ll also notice that digital signature information has been added by
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>jar
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">for
each file, of the form:
</FONT><P></DIV>
<font color="#990000"><PRE>Digest-Algorithms: SHA MD5
SHA-Digest: pDpEAG9NaeCx8aFtqPI4udSX/O0=
MD5-Digest: O4NcS1hE3Smnzlp2hj6qeg== </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
general, you don’t need to worry about any of this, and if you make
changes you can just modify your original manifest file and re-invoke
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>jar</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to create a new JAR file for your Bean. You can also add other Beans to the JAR
file simply by adding their information to your manifest.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">One
thing to notice is that you’ll probably want to put each Bean in its own
subdirectory, since when you create a JAR file you hand the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>jar</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
utility the name of a subdirectory and it puts everything in that subdirectory
into the JAR file. You can see that both
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Frog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
are in their own subdirectories.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Once
you have your Bean properly inside a JAR file you can bring it into a
Beans-enabled program-builder environment. The way you do this varies from one
tool to the next, but Sun provides a freely-available test bed for Java Beans
in their “Beans Development Kit” (BDK) called the “<A NAME="Index2251"></A><A NAME="Index2252"></A>beanbox.”
(Download the BDK from
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>www.javasoft.com</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.)
To place your Bean in the beanbox, copy the JAR file into the BDK’s
“jars” subdirectory before you start up the beanbox.
</FONT><a name="_Toc408018722"></a><P></DIV>
<A NAME="Heading459"></A><H3 ALIGN=LEFT>
More
complex Bean support
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can see how remarkably simple it is to make a Bean. But you aren’t
limited to what you’ve seen here. The Java Bean design provides a simple
point of entry but can also scale to more complex situations. These situations
are beyond the scope of this book but they will be briefly introduced here. You
can find more details at
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>http://java.sun.com/beans</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">One
place where you can add sophistication is with properties. The examples above
have shown only single properties, but it’s also possible to represent
multiple properties in an array. This is called an <A NAME="Index2253"></A><A NAME="Index2254"></A><A NAME="Index2255"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>indexed
property
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
You simply provide the appropriate methods (again following a naming convention
for the method names) and the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Introspector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
recognizes an indexed property so your application builder tool can respond
appropriately.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Properties
can be <A NAME="Index2256"></A><A NAME="Index2257"></A><A NAME="Index2258"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>bound</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which means that they will notify other objects via a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PropertyChangeEvent</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The other objects can then choose to change themselves based on the change to
the Bean.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Properties
can be <A NAME="Index2259"></A><A NAME="Index2260"></A><A NAME="Index2261"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>constrained</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which means that other objects can veto a change to that property if it is
unacceptable. The other objects are notified using a <A NAME="Index2262"></A><A NAME="Index2263"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PropertyChangeEvent</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and they can throw a <A NAME="Index2264"></A><A NAME="Index2265"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ProptertyVetoException</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to prevent the change from happening and to restore the old values.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can also change the way your Bean is represented at design time:
</FONT><P></DIV>
<OL>
<LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> You
can provide a <A NAME="Index2266"></A><A NAME="Index2267"></A>custom
property sheet for your particular Bean. The ordinary property sheet will be
used for all other Beans, but yours is automatically invoked when your Bean is
selected.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> You
can create a <A NAME="Index2268"></A><A NAME="Index2269"></A>custom
editor for a particular property, so the ordinary property sheet is used, but
when your special property is being edited, your editor will automatically be
invoked.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> You
can provide a <A NAME="Index2270"></A><A NAME="Index2271"></A>custom
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BeanInfo</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class for your Bean that produces information that’s different from the
default created by the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Introspector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> It’s
also possible to turn “expert” mode on and off in all <A NAME="Index2272"></A><A NAME="Index2273"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FeatureDescriptor</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
to distinguish between basic features and more complicated ones.
</FONT><a name="_Toc408018723"></a></OL><A NAME="Heading460"></A><H3 ALIGN=LEFT>
More
to Beans
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There’s
another issue that couldn’t be addressed here. Whenever you create a
Bean, you should expect that it will be run in a multithreaded environment.
This means that you must understand the issues of threading, which will be
introduced in the next chapter. You’ll find a section there called
“Java Beans revisited” that will look at the problem and its
solution.
</FONT><a name="_Toc408018724"></a><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0149.html">Prev</a> | <a href="tij0151.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -