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

📄 13_2.htm

📁 翁剀JAVA语言那门课程的教案 很多人都看多他的视频教程可惜没有ppt的教案
💻 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>Event and listener types</TITLE>
</HEAD>
<BODY>

<H2>
13.2 Event and listener types</H2>

<HR WIDTH="100%">
<BR>All the AWT components have been changed to include addXXXListener(
) and removeXXXListener( ) methods so that the appropriate types of listeners
can be added and removed from each component. You'll notice that the "XXX"
in each case also represents the argument for the method, for example,
addFooListener(FooListener fl). The following table includes the associated
events, listeners, methods, and the components that support those particular
events by providing the addXXXListener( ) and removeXXXListener( ) methods.
<BR>&nbsp;
<TABLE BORDER COLS=2 WIDTH="100%" >
<TR>
<TD>Event, listener interface and add-and&nbsp; remove-methods</TD>

<TD>Components supporting this event</TD>
</TR>

<TR>
<TD>ActionEvent&nbsp;
<BR>ActionListener&nbsp;
<BR>addActionListener( )&nbsp;
<BR>removeActionListener( )</TD>

<TD>Button, List, TextField, MenuItem, and its derivatives including CheckboxMenuItem,
Menu, and PopupMenu</TD>
</TR>

<TR>
<TD>AdjustmentEvent&nbsp;
<BR>AdjustmentListener&nbsp;
<BR>addAdjustmentListener( )&nbsp;
<BR>removeAdjustmentListener( )</TD>

<TD>Scrollbar Anything you create that&nbsp; implements the Adjustable&nbsp;
interface</TD>
</TR>

<TR>
<TD>ComponentEvent&nbsp;
<BR>ComponentListener&nbsp;
<BR>addComponentListener( )&nbsp;
<BR>removeComponentListener( )</TD>

<TD>Component and its derivatives,&nbsp; including Button, Canvas,&nbsp;
Checkbox, Choice, Container,&nbsp; Panel, Applet, ScrollPane,&nbsp; Window,
Dialog, FileDialog,&nbsp; Frame, Label, List, Scrollbar,&nbsp; TextArea,
and TextField</TD>
</TR>

<TR>
<TD>ContainerEvent&nbsp;
<BR>ContainerListener&nbsp;
<BR>addContainerListener( )&nbsp;
<BR>removeContainerListener( )</TD>

<TD>Container and its derivatives,&nbsp; including Panel, Applet,&nbsp;
ScrollPane, Window, Dialog,&nbsp; FileDialog, and Frame</TD>
</TR>

<TR>
<TD>FocusEvent&nbsp;
<BR>FocusListener&nbsp;
<BR>addFocusListener( )&nbsp;
<BR>removeFocusListener( )</TD>

<TD>Component and its derivatives,&nbsp; including Button, Canvas,&nbsp;
Checkbox, Choice, Container,&nbsp; Panel, Applet, ScrollPane,&nbsp; Window,
Dialog, FileDialog,&nbsp; Frame Label, List, Scrollbar,&nbsp; TextArea,
and TextField</TD>
</TR>

<TR>
<TD>KeyEvent&nbsp;
<BR>KeyListener&nbsp;
<BR>addKeyListener( )&nbsp;
<BR>removeKeyListener( )</TD>

<TD>Component and its derivatives,&nbsp; including Button, Canvas,&nbsp;
Checkbox, Choice, Container,&nbsp; Panel, Applet, ScrollPane,&nbsp; Window,
Dialog, FileDialog,&nbsp; Frame, Label, List, Scrollbar,&nbsp; TextArea,
and TextField</TD>
</TR>

<TR>
<TD>MouseEvent (for both clicks and motion)&nbsp;
<BR>MouseListener&nbsp;
<BR>addMouseListener( )&nbsp;
<BR>removeMouseListener( )</TD>

<TD>Component and its derivatives,&nbsp; including Button, Canvas,&nbsp;
Checkbox, Choice, Container,&nbsp; Panel, Applet, ScrollPane,&nbsp; Window,
Dialog, FileDialog,&nbsp; Frame, Label, List, Scrollbar,&nbsp; TextArea,
and TextField</TD>
</TR>

<TR>
<TD>MouseEvent (for both clicks and motion)&nbsp;
<BR>MouseMotionListener&nbsp;
<BR>addMouseMotionListener( )&nbsp;
<BR>removeMouseMotionListener( )</TD>

<TD>Component and its derivatives,&nbsp; including Button, Canvas,&nbsp;
Checkbox, Choice, Container,&nbsp;
<BR>Panel, Applet, ScrollPane, Window, Dialog, FileDialog, Frame, Label,
List, Scrollbar, TextArea, and TextField</TD>
</TR>

<TR>
<TD>WindowEvent&nbsp;
<BR>WindowListener&nbsp;
<BR>addWindowListener( )&nbsp;
<BR>removeWindowListener( )</TD>

<TD>Window and its derivatives, including Dialog, FileDialog, and Frame</TD>
</TR>

<TR>
<TD>ItemEvent&nbsp;
<BR>ItemListener&nbsp;
<BR>addItemListener( )&nbsp;
<BR>removeItemListener( )</TD>

<TD>Checkbox, CheckboxMenuItem, Choice, List, and anything that implements
the ItemSelectable interface</TD>
</TR>

<TR>
<TD>TextEvent&nbsp;
<BR>TextListener&nbsp;
<BR>addTextListener( )&nbsp;
<BR>removeTextListener( )</TD>

<TD>Anything derived from TextComponent, including TextArea and TextField</TD>
</TR>
</TABLE>


<P>You can see that each type of component supports only certain types
of events. It's helpful to see the events supported by each component,
as shown in the following table:
<BR>&nbsp;
<TABLE BORDER COLS=2 WIDTH="100%" >
<TR>
<TD>Component type&nbsp;</TD>

<TD>Events supported by this component</TD>
</TR>

<TR>
<TD>Adjustable</TD>

<TD>AdjustmentEvent</TD>
</TR>

<TR>
<TD>Applet</TD>

<TD>ContainerEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>Button</TD>

<TD>ActionEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>Canvas</TD>

<TD>FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>Checkbox</TD>

<TD>ItemEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>CheckboxMenuItem</TD>

<TD>ActionEvent, ItemEvent</TD>
</TR>

<TR>
<TD>Choice</TD>

<TD>ItemEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>Component</TD>

<TD>FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>Container</TD>

<TD>ContainerEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>Dialog</TD>

<TD>ContainerEvent, WindowEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>FileDialog</TD>

<TD>ContainerEvent, WindowEvent,FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>Frame</TD>

<TD>ContainerEvent, WindowEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>Label</TD>

<TD>FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>List</TD>

<TD>ActionEvent, FocusEvent, KeyEvent, MouseEvent, ItemEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>Menu</TD>

<TD>ActionEvent</TD>
</TR>

<TR>
<TD>MenuItem</TD>

<TD>ActionEvent</TD>
</TR>

<TR>
<TD>Panel</TD>

<TD>ContainerEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>PopupMenu</TD>

<TD>ActionEvent</TD>
</TR>

<TR>
<TD>Scrollbar</TD>

<TD>AdjustmentEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>ScrollPane</TD>

<TD>ContainerEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>TextArea</TD>

<TD>TextEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>TextComponent</TD>

<TD>TextEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>TextField</TD>

<TD>ActionEvent, TextEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent</TD>
</TR>

<TR>
<TD>Window</TD>

<TD>ContainerEvent, WindowEvent,
<BR>FocusEvent, KeyEvent, MouseEvent,
<BR>ComponentEvent</TD>
</TR>
</TABLE>
&nbsp;
<BR>Once you know which events a particular component supports, you don't
need to look anything up to react to that event. You simply:
<OL>
<LI>
Take the name of the event class and remove the word "Event." Add the word
"Listener" to what remains. This is the listener interface you need to
implement in your inner class.</LI>

<LI>
Implement the interface above and write out the methods for the events
you want to capture. For example, you might be looking for mouse movements,
so you write code for the mouseMoved( ) method of the MouseMotionListener
interface. (You must implement the other methods, of course, but there's
a shortcut for that which you'll see soon.)</LI>

<LI>
Create an object of the listener class in step 2. Register it with your
component with the method produced by prefixing "add" to your listener
name. For example, addMouseMotionListener( ).</LI>
</OL>
To finish what you need to know, here are the listener interfaces:
<BR>&nbsp;
<TABLE BORDER COLS=2 WIDTH="100%" >
<TR>
<TD>Listener interface
<BR>w/ adapter</TD>

<TD>Methods in interface</TD>
</TR>

<TR>
<TD>ActionListener</TD>

<TD>actionPerformed(ActionEvent)</TD>
</TR>

<TR>
<TD>AdjustmentListener</TD>

<TD>adjustmentValueChanged(AdjustmentEvent)</TD>
</TR>

<TR>
<TD>ComponentListener
<BR>ComponentAdapter</TD>

<TD>componentHidden(ComponentEvent)
<BR>componentShown(ComponentEvent)
<BR>componentMoved(ComponentEvent)
<BR>componentResized(ComponentEvent)</TD>
</TR>

<TR>
<TD>ContainerListener
<BR>ContainerAdapter</TD>

<TD>componentAdded(ContainerEvent)
<BR>componentRemoved(ContainerEvent)</TD>
</TR>

<TR>
<TD>FocusListener
<BR>FocusAdapter</TD>

<TD>focusGained(FocusEvent)
<BR>focusLost(FocusEvent)</TD>
</TR>

<TR>
<TD>KeyListener
<BR>KeyAdapter</TD>

<TD>keyPressed(KeyEvent)
<BR>keyReleased(KeyEvent)
<BR>keyTyped(KeyEvent)</TD>
</TR>

<TR>
<TD>MouseListener
<BR>MouseAdapter</TD>

<TD>mouseClicked(MouseEvent)
<BR>mouseEntered(MouseEvent)
<BR>mouseExited(MouseEvent)
<BR>mousePressed(MouseEvent)
<BR>mouseReleased(MouseEvent)</TD>
</TR>

<TR>
<TD>MouseMotionListener
<BR>MouseMotionAdapter</TD>

<TD>mouseDragged(MouseEvent)
<BR>mouseMoved(MouseEvent)</TD>
</TR>

<TR>
<TD>WindowListener
<BR>WindowAdapter</TD>

<TD>windowOpened(WindowEvent)
<BR>windowClosing(WindowEvent)
<BR>windowClosed(WindowEvent)
<BR>windowActivated(WindowEvent)
<BR>windowDeactivated(WindowEvent)
<BR>windowIconified(WindowEvent)
<BR>windowDeiconified(WindowEvent)</TD>
</TR>

<TR>
<TD>ItemListener&nbsp;</TD>

<TD>itemStateChanged(ItemEvent)</TD>
</TR>

<TR>
<TD>TextListener</TD>

<TD>textValueChanged(TextEvent)</TD>
</TR>
</TABLE>


<P>In the table above, you can see that some listener interfaces have only
one method. These are trivial to implement since you'll implement them
only when you want to write that particular method. However, the listener
interfaces that have multiple methods could be less pleasant to use. For
example, something you must always do when creating an application is provide
a WindowListener to the Frame so that when you get the windowClosing( )
event you can call System.exit(0) to exit the application. But since WindowListener
is an interface, you must implement all of the other methods even if they
don't do anything. This can be annoying.

<P>To solve the problem, each of the listener interfaces that have more
than one method are provided with adapters, the names of which you can
see in the table above. Each adapter provides default methods for each
of the interface methods. (Alas, WindowAdapter does not have a default
windowClosing( ) that calls System.exit(0).) Then all you need to do is
inherit from the adapter and override only the methods you need to change.
For example, the typical WindowListener you'll use looks like this:
<UL>
<PRE>class MyWindowListener extends WindowAdapter {</PRE>

<PRE>&nbsp;&nbsp;&nbsp; public void windowClosing(WindowEvent e) {</PRE>

<PRE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.exit(0);</PRE>

<PRE>&nbsp;&nbsp;&nbsp; }</PRE>

<PRE>}</PRE>
</UL>
The whole point of the adapters is to make the creation of listener classes
easy.

<P>There is a downside to adapters, however, in the form of a pitfall.
Suppose you write a WindowAdapter like the one above:
<UL>
<PRE>class MyWindowListener extends WindowAdapter {</PRE>

<PRE>&nbsp;&nbsp;&nbsp; public void WindowClosing(WindowEvent e) {</PRE>

<PRE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.exit(0);</PRE>

<PRE>&nbsp;&nbsp;&nbsp; }</PRE>

<PRE>}</PRE>
</UL>
This doesn't work, but it will drive you crazy trying to figure out why,
since everything will compile and run fine -- except that closing the window
won't exit the program. Can you see the problem? It's in the name of the
method: WindowClosing( ) instead of windowClosing( ). A simple slip in
capitalization results in the addition of a completely new method. However,
this is not the method that's called when the window is closing, so you
don't get the desired results.

<P>
<HR WIDTH="100%">
<DIV ALIGN=right><A HREF="13_3.htm">Next Page</A></DIV>

</BODY>
</HTML>

⌨️ 快捷键说明

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