📄 tij0150.html
字号:
<font color="#0000ff">public</font> <font color="#0000ff">void</font> croak()
<font color="#0000ff">public</font> <font color="#0000ff">void</font> removeActionListener(ActionListener)
<font color="#0000ff">public</font> <font color="#0000ff">void</font> addActionListener(ActionListener)
<font color="#0000ff">public</font> <font color="#0000ff">int</font> getJumps()
<font color="#0000ff">public</font> <font color="#0000ff">void</font> setColor(Color)
<font color="#0000ff">public</font> <font color="#0000ff">void</font> setSpots(Spots)
<font color="#0000ff">public</font> <font color="#0000ff">void</font> setJumper(<font color="#0000ff">boolean</font>)
<font color="#0000ff">public</font> <font color="#0000ff">boolean</font> isJumper()
<font color="#0000ff">public</font> <font color="#0000ff">void</font> addKeyListener(KeyListener)
<font color="#0000ff">public</font> Color getColor()
<font color="#0000ff">public</font> <font color="#0000ff">void</font> removeKeyListener(KeyListener)
<font color="#0000ff">public</font> Spots getSpots()
======================
Event support:
Listener type:
KeyListener
Listener method:
keyTyped
Listener method:
keyPressed
Listener method:
keyReleased
Method descriptor:
<font color="#0000ff">public</font> <font color="#0000ff">void</font> keyTyped(KeyEvent)
Method descriptor:
<font color="#0000ff">public</font> <font color="#0000ff">void</font> keyPressed(KeyEvent)
Method descriptor:
<font color="#0000ff">public</font> <font color="#0000ff">void</font> keyReleased(KeyEvent)
Add Listener Method:
<font color="#0000ff">public</font> <font color="#0000ff">void</font> addKeyListener(KeyListener)
Remove Listener Method:
<font color="#0000ff">public</font> <font color="#0000ff">void</font> removeKeyListener(KeyListener)
====================
Listener type:
ActionListener
Listener method:
actionPerformed
Method descriptor:
<font color="#0000ff">public</font> <font color="#0000ff">void</font> actionPerformed(ActionEvent)
Add Listener Method:
<font color="#0000ff">public</font> <font color="#0000ff">void</font> addActionListener(ActionListener)
Remove Listener Method:
<font color="#0000ff">public</font> <font color="#0000ff">void</font> removeActionListener(ActionListener)
====================</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
reveals most of what the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Introspector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
sees as it produces a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BeanInfo</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object from your Bean. You can see that the type of the property and its name
are independent. Notice the lowercasing of the property name. (The only time
this doesn’t occur is when the property name begins with more than one
capital letter in a row.) And remember that the method names you’re
seeing here (such as the read and write methods) are actually produced from a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Method</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object that can be used to invoke the associated method on the object.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
public method list includes the methods that are not associated with a property
or event, such as
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>croak( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
as well as those that are. These are all the methods that you can call
programmatically for a Bean, and the application builder tool can choose to
list all of these while you’re making method calls, to ease your task.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Finally,
you can see that the events are fully parsed out into the listener, its
methods, and the add- and remove-listener methods. Basically, once you have the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BeanInfo</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you can find out everything of importance for the Bean. You can also call the
methods for that Bean, even though you don’t have any other information
except the object (again, a feature of reflection).
</FONT><a name="_Toc408018720"></a><P></DIV>
<A NAME="Heading457"></A><H3 ALIGN=LEFT>
A
more sophisticated Bean
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
also has its own <A NAME="Index2233"></A><A NAME="Index2234"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>addActionListener( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and <A NAME="Index2235"></A><A NAME="Index2236"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>removeActionListener( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
so you can attach your own listener that will be fired when the user clicks on
the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
You should be able to recognize the property and event support:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: BangBean.java</font>
<font color="#009900">// A graphical Bean</font>
<font color="#0000ff">package</font> bangbean;
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">import</font> java.awt.event.*;
<font color="#0000ff">import</font> java.io.*;
<font color="#0000ff">import</font> java.util.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> BangBean <font color="#0000ff">extends</font> Canvas
<font color="#0000ff">implements</font> Serializable {
<font color="#0000ff">protected</font> <font color="#0000ff">int</font> xm, ym;
<font color="#0000ff">protected</font> <font color="#0000ff">int</font> cSize = 20; <font color="#009900">// Circle size</font>
<font color="#0000ff">protected</font> String text = "Bang!";
<font color="#0000ff">protected</font> <font color="#0000ff">int</font> fontSize = 48;
<font color="#0000ff">protected</font> Color tColor = Color.red;
<font color="#0000ff">protected</font> ActionListener actionListener;
<font color="#0000ff">public</font> BangBean() {
addMouseListener(<font color="#0000ff">new</font> ML());
addMouseMotionListener(<font color="#0000ff">new</font> MML());
}
<font color="#0000ff">public</font> <font color="#0000ff">int</font> getCircleSize() { <font color="#0000ff">return</font> cSize; }
<font color="#0000ff">public</font> <font color="#0000ff">void</font> setCircleSize(<font color="#0000ff">int</font> newSize) {
cSize = newSize;
}
<font color="#0000ff">public</font> String getBangText() { <font color="#0000ff">return</font> text; }
<font color="#0000ff">public</font> <font color="#0000ff">void</font> setBangText(String newText) {
text = newText;
}
<font color="#0000ff">public</font> <font color="#0000ff">int</font> getFontSize() { <font color="#0000ff">return</font> fontSize; }
<font color="#0000ff">public</font> <font color="#0000ff">void</font> setFontSize(<font color="#0000ff">int</font> newSize) {
fontSize = newSize;
}
<font color="#0000ff">public</font> Color getTextColor() { <font color="#0000ff">return</font> tColor; }
<font color="#0000ff">public</font> <font color="#0000ff">void</font> setTextColor(Color newColor) {
tColor = newColor;
}
<font color="#0000ff">public</font> <font color="#0000ff">void</font> paint(Graphics g) {
g.setColor(Color.black);
g.drawOval(xm - cSize/2, ym - cSize/2,
cSize, cSize);
}
<font color="#009900">// This is a unicast listener, which is</font>
<font color="#009900">// the simplest form of listener management:</font>
<font color="#0000ff">public</font> <font color="#0000ff">void</font> addActionListener (
ActionListener l)
<font color="#0000ff">throws</font> TooManyListenersException {
<font color="#0000ff">if</font>(actionListener != <font color="#0000ff">null</font>)
<font color="#0000ff">throw</font> <font color="#0000ff">new</font> TooManyListenersException();
actionListener = l;
}
<font color="#0000ff">public</font> <font color="#0000ff">void</font> removeActionListener(
ActionListener l) {
actionListener = <font color="#0000ff">null</font>;
}
<font color="#0000ff">class</font> ML <font color="#0000ff">extends</font> MouseAdapter {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> mousePressed(MouseEvent e) {
Graphics g = getGraphics();
g.setColor(tColor);
g.setFont(
<font color="#0000ff">new</font> Font(
"TimesRoman", Font.BOLD, fontSize));
<font color="#0000ff">int</font> width =
g.getFontMetrics().stringWidth(text);
g.drawString(text,
(getSize().width - width) /2,
getSize().height/2);
g.dispose();
<font color="#009900">// Call the listener's method:</font>
<font color="#0000ff">if</font>(actionListener != <font color="#0000ff">null</font>)
actionListener.actionPerformed(
<font color="#0000ff">new</font> ActionEvent(BangBean.<font color="#0000ff">this</font>,
ActionEvent.ACTION_PERFORMED, <font color="#0000ff">null</font>));
}
}
<font color="#0000ff">class</font> MML <font color="#0000ff">extends</font> MouseMotionAdapter {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> mouseMoved(MouseEvent e) {
xm = e.getX();
ym = e.getY();
repaint();
}
}
<font color="#0000ff">public</font> Dimension getPreferredSize() {
<font color="#0000ff">return</font> <font color="#0000ff">new</font> Dimension(200, 200);
}
<font color="#009900">// Testing the BangBean:</font>
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
BangBean bb = <font color="#0000ff">new</font> BangBean();
<font color="#0000ff">try</font> {
bb.addActionListener(<font color="#0000ff">new</font> BBL());
} <font color="#0000ff">catch</font>(TooManyListenersException e) {}
Frame aFrame = <font color="#0000ff">new</font> Frame("BangBean Test");
aFrame.addWindowListener(
<font color="#0000ff">new</font> WindowAdapter() {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(bb, BorderLayout.CENTER);
aFrame.setSize(300,300);
aFrame.setVisible(<font color="#0000ff">true</font>);
}
<font color="#009900">// During testing, send action information</font>
<font color="#009900">// to the console:</font>
<font color="#0000ff">static</font> <font color="#0000ff">class</font> BBL <font color="#0000ff">implements</font> ActionListener {
<font color="#0000ff">public</font> <font color="#0000ff">void</font> actionPerformed(ActionEvent e) {
System.out.println("BangBean action");
}
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
first thing you’ll notice is that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
implements the <A NAME="Index2237"></A><A NAME="Index2238"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Serializable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
interface. This means that the application builder tool can
“pickle” all the information 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">
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.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can see that all the fields are
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>private</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which is what you’ll usually do with a Bean – allow access only
through methods, usually using the “property” scheme.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
you look at the signature for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>addActionListener( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you’ll see that it can throw a <A NAME="Index2239"></A><A NAME="Index2240"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TooManyListenersException</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This indicates that it is <A NAME="Index2241"></A><A NAME="Index2242"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>unicast</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which means it notifies only one listener when the event occurs. Ordinarily,
you’ll use <A NAME="Index2243"></A><A NAME="Index2244"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>multicast</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
you press the mouse, the text is put in the middle of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BangBean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and if the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -