mouselistener.html
来自「jsf、swing的官方指南」· HTML 代码 · 共 874 行 · 第 1/3 页
HTML
874 行
<div class="linkAHEAD"><a href="generalrules.html">General Information about Writing Event Listeners</a></div><div class="linkAHEAD"><a href="eventsandcomponents.html">Listeners Supported by Swing Components</a></div><div class="linkAHEAD"><a href="handling.html">Implementing Listeners for Commonly Handled Events</a></div><div class="linkBHEAD"><a href="actionlistener.html">How to Write an Action Listener</a></div><div class="linkBHEAD"><a href="caretlistener.html">How to Write a Caret Listener</a></div><div class="linkBHEAD"><a href="changelistener.html">How to Write a Change Listener</a></div><div class="linkBHEAD"><a href="componentlistener.html">How to Write a Component Listener</a></div><div class="linkBHEAD"><a href="containerlistener.html">How to Write a Container Listener</a></div><div class="linkBHEAD"><a href="documentlistener.html">How to Write a Document Listener</a></div><div class="linkBHEAD"><a href="focuslistener.html">How to Write a Focus Listener</a></div><div class="linkBHEAD"><a href="internalframelistener.html">How to Write an Internal Frame Listener</a></div><div class="linkBHEAD"><a href="itemlistener.html">How to Write an Item Listener</a></div><div class="linkBHEAD"><a href="keylistener.html">How to Write a Key Listener</a></div><div class="linkBHEAD"><a href="listdatalistener.html">How to Write a List Data Listener</a></div><div class="linkBHEAD"><a href="listselectionlistener.html">How to Write a List Selection Listener</a></div><div class="nolinkBHEAD">How to Write a Mouse Listener</div><div class="linkBHEAD"><a href="mousemotionlistener.html">How to Write a Mouse-Motion Listener</a></div><div class="linkBHEAD"><a href="mousewheellistener.html">How to Write a Mouse-Wheel Listener</a></div><div class="linkBHEAD"><a href="propertychangelistener.html">How to Write a Property Change Listener</a></div><div class="linkBHEAD"><a href="tablemodellistener.html">How to Write a Table Model Listener</a></div><div class="linkBHEAD"><a href="treeexpansionlistener.html">How to Write a Tree Expansion Listener</a></div><div class="linkBHEAD"><a href="treemodellistener.html">How to Write a Tree Model Listener</a></div><div class="linkBHEAD"><a href="treeselectionlistener.html">How to Write a Tree Selection Listener</a></div><div class="linkBHEAD"><a href="treewillexpandlistener.html">How to Write a Tree-Will-Expand Listener</a></div><div class="linkBHEAD"><a href="undoableeditlistener.html">How to Write an Undoable Edit Listener</a></div><div class="linkBHEAD"><a href="windowlistener.html">How to Write Window Listeners</a></div><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=listselectionlistener.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=mousemotionlistener.html>Next »</a> </div> <div id=PageTitle>How to Write a Mouse Listener</div> <blockquote>Mouse events tell you when the user uses the mouse(or similar input device)to interact with a component.Mouse events occur when the cursor enters or exits a component's onscreen areaand when the user presses or releases one ofthe mouse buttons.<p>Tracking the cursor's motioninvolves significantly more system overheadthan tracking other mouse events, somouse-motion events are separated into a separate listener type (see <a href="mousemotionlistener.html">How toWrite a Mouse Motion Listener</a>).<p>To track mouse wheel events, you can registera mouse-wheel listener. See <a href="mousewheellistener.html">How to Write a Mouse Wheel Listener</a> for more information.<p>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>. It does not implementthe <code>MouseWheelListener</code> interface.<font color=red>[Footnote: <code>MouseInputAdapter</code>doesn't directly implement <code>MouseListener</code> and<code>MouseMotionListener</code>. Instead, it implements<code>MouseInputListener</code>, a convenience interface thatimplements <code>MouseListener</code> and<code>MouseMotionListener</code>.]</font> <p>The following example shows a mouse listener.At the top of the window is a blank area(implemented, strangely enough, by a class named <code>BlankArea</code>).The mouse listener listens for events both on the <code>BlankArea</code>and on its container,an instance of <code>MouseEventDemo</code>.Each time a mouse event occurs,a descriptive message is displayed under the blank area.By moving the cursor on top of the blank areaand occasionally pressing mouse buttons,you can fire mouse events.<p><p><center><IMG SRC="../../figures/uiswing/events/MouseEventDemo.png" WIDTH="412" HEIGHT="287" 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/MouseEventDemo.jnlp">Run MouseEventDemo</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#MouseEventDemo">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-entered events.<li> Press and hold the mouse button. <br> You'll see a mouse-pressed event. You might see some extra mouse events, such as mouse-exited and then mouse-entered.<li> Release the mouse button. <br> You'll see a mouse-released event. If you didn't move the mouse, a mouse-clicked event will follow.<li> Press and hold the mouse button, and then drag the mouse so that the cursor ends up outside the window. Release the mouse button. <br> You'll see a mouse-pressed event, followed by a mouse-exited event, followed by a mouse-released event. You are <em>not</em> notified of the cursor's motion. To get mouse-motion events, you need to implement a <a href="mousemotionlistener.html">mouse-motion listener.</a></ol><hr></blockquote>You can find the demo's code in<a class="SourceLink" target="_blank" href="examples/MouseEventDemo.java"><code>MouseEventDemo.java</code></a>and<a class="SourceLink" target="_blank" href="examples/BlankArea.java"><code>BlankArea.java</code></a>.Here is the demo's mouse event handling code:<blockquote><pre>public class MouseEventDemo ... implements MouseListener { <em>//where initialization occurs:</em> //Register for mouse events on blankArea and the panel. blankArea.addMouseListener(this); addMouseListener(this); ... public void mousePressed(MouseEvent e) { saySomething("Mouse pressed; # of clicks: " + e.getClickCount(), e); } public void mouseReleased(MouseEvent e) { saySomething("Mouse released; # of clicks: " + e.getClickCount(), e); } public void mouseEntered(MouseEvent e) { saySomething("Mouse entered", e); } public void mouseExited(MouseEvent e) { saySomething("Mouse exited", e); } public void mouseClicked(MouseEvent e) { saySomething("Mouse clicked (# of clicks: " + e.getClickCount() + ")", e); } void saySomething(String eventDescription, MouseEvent e) { textArea.append(eventDescription + " detected on " + e.getComponent().getClass().getName() + "." + newline); }}</pre></blockquote></blockquote><h3><a name="api">The Mouse Listener API</a></h3><blockquote><p align=center><a name="mouselistener">The MouseListener 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/MouseAdapter.html"><code>MouseAdapter</code></a>. You can also use the Swing adapter class,<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/event/MouseInputAdapter.html"><code>MouseInputAdapter</code></a>, which has all the methods available from <code>MouseListener</code>and <code>MouseMotionListener</code>.</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/MouseListener.html#mouseClicked(java.awt.event.MouseEvent)">mouseClicked(MouseEvent)</a></td><td> Called just after the user clicks the listened-to component.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseListener.html#mouseEntered(java.awt.event.MouseEvent)">mouseEntered(MouseEvent)</a></td><td> Called just after the cursor enters the bounds of the listened-to component.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseListener.html#mouseExited(java.awt.event.MouseEvent)">mouseExited(MouseEvent)</a></td><td> Called just after the cursor exits the bounds of the listened-to component.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseListener.html#mousePressed(java.awt.event.MouseEvent)">mousePressed(MouseEvent)</a></td><td> Called just after the user presses a mouse button while the cursor is over the listened-to component.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseListener.html#mouseReleased(java.awt.event.MouseEvent)">mouseReleased(MouseEvent)</a></td><td> Called just after the user releases a mouse button after a mouse press over the listened-to component.</td></tr></table><p align=center><a name="mouseevent">The MouseEvent Class</a><p><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/MouseEvent.html#getClickCount()">int getClickCount()</a></td><td>Return the number of quick, consecutive clicks the user has made (including this event). For example, returns 2 for a double click.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseEvent.html#getX()">int getX()</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseEvent.html#getY()">int getY()</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseEvent.html#getPoint()">Point getPoint()</a></td><td>Return the (x,y) position at which the event occurred, relative to the component that fired the event.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/event/MouseEvent.html#getButton()">int getButton()</a></td><td>Return which mouse button, if any, has changed state. One of the following constants is returned: <code>NOBUTTON</code>, <code>BUTTON1</code>, <code>BUTTON2</code>, or <code>BUTTON3</code>. Introduced in release 1.4.</td></tr><tr><td>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?