📄 14_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>A more sophisticated Bean</TITLE>
</HEAD>
<BODY>
<H2>
14.3 A more sophisticated Bean</H2>
<HR WIDTH="100%">
<BR>This next example is slightly more sophisticated, albeit frivolous.
It's a canvas that draws a little circle around the mouse whenever the
mouse is moved. When you press the mouse, the word "Bang!" appears in the
middle of the screen, and an action listener is fired.
<P>The properties you can change are the size of the circle as well as
the color, size, and text of the word that is displayed when you press
the mouse. A BangBean also has its own addActionListener( ) and removeActionListener(
) so you can attach your own listener that will be fired when the user
clicks on the BangBean. You should be able to recognize the property and
event support:
<P>Case Study: <A HREF="case/BangBean.java">BangBean.java</A>
<P>The first thing you'll notice is that BangBean implements the Serializable
interface. This means that the application builder tool can "pickle" all
the information for the BangBean using serialization after the program
designer has adjusted the values of the properties. When the Bean is created
as part of the running application, these "pickled" properties are restored
so that you get exactly what you designed.
<P>You can see that all the fields are private, which is what you'll usually
do with a Bean -- allow access only through methods, usually using the
"property" scheme.
<P>When you look at the signature for addActionListener( ), you'll see
that it can throw a TooManyListenersException. This indicates that it is
unicast, which means it notifies only one listener when the event occurs.
Ordinarily, you'll use multicast events so that many listeners can be notified
of an event. However, that runs into issues that you won't be ready for
until the next chapter, so it will be revisited there (under the heading
"Java Beans revisited"). A unicast event sidesteps the problem.
<P>When you press the mouse, the text is put in the middle of the BangBean,
and if the actionListener field is not null, its actionPerformed( ) is
called, creating a new ActionEvent 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). The main( ) is
added to allow you to test the program from the command line. When a Bean
is in a development environment, main( ) will not be used, but it's helpful
to have a main( ) in each of your Beans because it provides for rapid testing.
main( ) creates a Frame and places a BangBean within it, attaching a simple
ActionListener to the BangBean to print to the console whenever an ActionEvent
occurs. Usually, of course, the application builder tool would create most
of the code that uses the Bean.
<P>When you run the BangBean through BeanDumper or put the BangBean 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 BangBean is inherited from Canvas, and Canvas is a Bean, so you're
seeing its properties and events as well.
<BR>
<HR WIDTH="100%">
<DIV ALIGN=right><A HREF="14_4.htm">Next Page</A></DIV>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -