mousemotionlistener.html
来自「jsf、swing的官方指南」· HTML 代码 · 共 635 行 · 第 1/2 页
HTML
635 行
<div class="linkAHEAD"><a href="api.html">Listener API Table</a></div><div class="linkAHEAD"><a href="problems.html">Solving Common Event-Handling Problems</a></div></div> </div> <div id=MainFlow class=MainFlow_indented> <span id=BreadCrumbs> <a href=../../index.html target=_top>Home Page</a> > <a href=../index.html target=_top>Creating a GUI with JFC/Swing</a> > <a href=index.html target=_top>Writing Event Listeners</a> </span> <div class=NavBit> <a target=_top href=mouselistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=mousewheellistener.html>Next »</a> </div> <div id=PageTitle>How to Write a Mouse-Motion Listener</div> <blockquote>Mouse-motion events tell you when the user uses the mouse(or a similar input device) to move the onscreen cursor.For information on listening for other kinds of mouse events,such as clicks,see <a href="mouselistener.html">How to Write a Mouse Listener</a>.For information on listening for mouse-wheel events,see <a href="mousewheellistener.html">How to Write a MouseWheel Listener</a>.If your program needs to detect both mouse eventsand mouse-motion events,you can use Swing's convenient <code>MouseInputAdapter</code> class,which implements both <code>MouseListener</code> and<code>MouseMotionListener</code>.<p>The following demo contains a mouse-motion listener.It's exactly like the example in <a href="mouselistener.html">How to Write a Mouse Listener</a>,except for substituting <code>MouseMotionListener</code>for <code>MouseListener</code>,implementing the <code>mouseDragged</code> and<code>mouseMoved</code> methods instead of the mouse listener methods,and displaying coordinates instead of numbers of clicks.<p><p><center><IMG SRC="../../figures/uiswing/events/MouseMotionEventDemo.png" WIDTH="413" HEIGHT="261" ALIGN="BOTTOM" ALT=""></center></p><p><blockquote><hr><strong>Try this:</strong> <ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/events/examples/MouseMotionEventDemo.jnlp">Run MouseMotionEventDemo</a> using <a href=http://java.sun.com/products/javawebstart> Java<sup><font size=-2>TM</font></sup> Web Start</a>. Or, to compile and run the example yourself, consult the <a href="examples/index.html#MouseMotionEventDemo">example index</a>.<li> Move the cursor into the yellow rectangle at the top of the window. <br> You'll see one or more mouse-moved events.<li> Press and hold the mouse button, and then move the mouse so that the cursor is outside the yellow rectangle. <br> You'll see mouse-dragged events.</ol><hr></blockquote>Here is the code from MouseMotionEventDemo.java thatimplements the mouse-motion event handling:<blockquote><pre>public class MouseMotionEventDemo extends JPanel implements MouseMotionListener { <em>//...in initialization code:</em> //Register for mouse events on blankArea and panel. blankArea.addMouseMotionListener(this); addMouseMotionListener(this); ... } public void mouseMoved(MouseEvent e) { saySomething("Mouse moved", e); } public void mouseDragged(MouseEvent e) { saySomething("Mouse dragged", e); } void saySomething(String eventDescription, MouseEvent e) { textArea.append(eventDescription + " (" + e.getX() + "," + e.getY() + ")" + " detected on " + e.getComponent().getClass().getName() + newline); }}</pre></blockquote><p>A more interesting example is <code>SelectionDemo</code>,which is discussed in <a class="TutorialLink" target="_top" href="../painting/concepts2.html">Introduction to Painting Concepts</a>.The program draws a rectangle illustrating the user's current dragging.To do this, it must implement an event handler for three kinds of mouse events:mouse presses, mouse drags, and mouse releases.To be informed of all these events,the handler must implement both the<code>MouseListener</code> and <code>MouseMotionListener</code> interfaces,and be registered as both a mouse listener anda mouse-motion listener.To avoid having to define empty methods,the handler doesn't implement either listener interface directly.Instead, it extends <code>MouseInputAdapter</code>,as the following code snippet shows.<p><font color=red>[PENDING: When this example has beenupdated, check the code.]</font><blockquote><pre><em>...//where initialization occurs:</em> MyListener myListener = new MyListener(); addMouseListener(myListener); addMouseMotionListener(myListener);...class MyListener extends MouseInputAdapter { public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); currentRect = new Rectangle(x, y, 0, 0); updateDrawableRect(getWidth(), getHeight()); repaint(); } public void mouseDragged(MouseEvent e) { updateSize(e); } public void mouseReleased(MouseEvent e) { updateSize(e); } void updateSize(MouseEvent e) { int x = e.getX(); int y = e.getY(); ... repaint(...); }}</pre></blockquote></blockquote><h3><a name="api">The Mouse-Motion Listener API</a></h3><blockquote><p align=center><a name="mousemotionlistener">The MouseMotionListener Interface</a><p><em>The corresponding adapter class is<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseMotionAdapter.html"><code>MouseMotionAdapter</code></a>.</em><table border=1><tr><th align=left>Method</th><th align=left>Purpose</th></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseMotionListener.html#mouseDragged(java.awt.event.MouseEvent)">mouseDragged(MouseEvent)</a></td><td> Called in response to the user moving the mouse while holding a mouse button down. This event is fired by the component that fired the most recent mouse-pressed event, even if the cursor is no longer over that component.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseMotionListener.html#mouseMoved(java.awt.event.MouseEvent)">mouseMoved(MouseEvent)</a></td><td> Called in response to the user moving the mouse with no mouse buttons pressed. This event is fired by the component that's currently under the cursor.</td></tr></table><p>Each mouse-motion event method has a single parameter —and it's <em>not</em> called <code>MouseMotionEvent</code>!Instead, each mouse-motion event method uses a<code>MouseEvent</code> argument.See <a href="mouselistener.html#mouseevent">The MouseEvent API</a>for information about using <code>MouseEvent</code> objects.</blockquote><a name="eg"><h3>Examples that Use Mouse-Motion Listeners</h3></a><blockquote>The following table lists theexamples that use mouse-motion listeners.<p><table><tr><th align=left> Example</th><th align=left> Where Described</th><th align=left> Notes </th></tr><tr valign=top><td> <a href="examples/index.html#MouseMotionEventDemo"><code>MouseMotionEventDemo</code></a></td><td> This section</td><td> Reports all mouse motion events that occur within a blank panel to demonstrate the circumstances under which mouse motion events are fired.</td></tr><tr valign=top><td> <a href="../components/examples/index.html#LayeredPaneDemo"><code>LayeredPaneDemo</code></a> and <br> <a href="../components/examples/index.html#LayeredPaneDemo2"><code>LayeredPaneDemo2</code></a></td><td><a class="TutorialLink" target="_top" href="../components/layeredpane.html">How to Use Layered Panes</a></td><td> Moves an image of Duke around within a layered pane in response to mouse motion events.</td></tr><tr valign=top><td><a href="../painting/examples/index.html#SelectionDemo"><code>SelectionDemo</code></a></td><td><a class="TutorialLink" target="_top" href="../painting/concepts2.html">Introduction to Painting Concepts</a></td><td> Lets the user drag a rectangle to select a portion of an image. Uses a subclass of <code>MouseInputAdapter</code> to listen to both mouse events and mouse-motion events.</td></tr><tr valign=top><td><a href="../components/examples/index.html#GlassPaneDemo"><code>GlassPaneDemo</code></a></td><td><a class="TutorialLink" target="_top" href="../components/rootpane.html">How to Use Root Panes</a></td><td> Uses a subclass of <code>MouseInputAdapter</code> to listen to mouse events and mouse-motion events on the root pane's glass pane. Redispatches the events to underlying components.</td></tr> <tr valign=top><td><a href="../components/examples/index.html#ScrollDemo"><code>ScrollDemo</code></a></td><td><a class="TutorialLink" target="_top" href="../components/scrollpane.html">How to Use Scroll Panes</a></td><td> The label subclass, ScrollablePicture, uses a mouse-motion listener to allow the user to scroll the picture even when the user drags outside the window.</td></tr></table> </blockquote> <div class=NavBit> <a target=_top href=mouselistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=mousewheellistener.html>Next »</a> </div> </div> <div id=Footer><div id=TagNotes> Problems with the examples? Try <a target="_blank" href=../../information/run-examples.html>Compiling and Running the Examples: FAQs</a>. <br> Complaints? Compliments? Suggestions? <a target="_blank" href="http://developer.sun.com/contact/tutorial_feedback.jsp">Give us your feedback</a>.<br><br> <a target="_blank" href="../../information/copyright.html">Copyright</a> 1995-2006 Sun Microsystems, Inc. All rights reserved. <span id=Download></span></div> </div> <div class=PrintHeaders> <b>Previous page:</b> How to Write a Mouse Listener <br><b>Next page:</b> How to Write a Mouse-Wheel Listener </div> </body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?