Java Tutorial/Swing Event/MouseMotionListener

Материал из Java эксперт
Перейти к: навигация, поиск

Demonstrates mouse motion events

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
class CustomListener implements MouseMotionListener {
  public void mouseMoved(MouseEvent me) {
    System.out.println("mouseMoved");
  }
  public void mouseDragged(MouseEvent me) {
    System.out.println("mouseDragged");
  }
}
public class MouseMotionListenerDemo {
  public static void main(String[] a) {
    JFrame frame = new JFrame("Popup JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel label = new JLabel();
    label.addMouseMotionListener(new CustomListener() {
    });
    frame.add(label);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





Handle mouse motion event

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Main extends JFrame {
  public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 400);
    JTextArea textArea = new JTextArea("drag it...");
    textArea.addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseDragged(MouseEvent e) {
        System.out.println("Mouse Dragged...");
      }
      public void mouseMoved(MouseEvent e) {
        System.out.println("Mouse Moved...");
      }
    });
    getContentPane().add(textArea);
  }
  public static void main(String[] args) {
    new Main().setVisible(true);
  }
}





How to Write a Mouse-Motion Listener

/*
 *
 * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class MouseMotionEventDemo extends JPanel implements MouseMotionListener {
  BlankArea blankArea = new BlankArea(new Color(0.98f, 0.97f, 0.85f));
  JTextArea textArea = new JTextArea();
  public MouseMotionEventDemo() {
    super(new BorderLayout());
    add(blankArea,"North");
    
    textArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollPane.setPreferredSize(new Dimension(200, 75));
    add(scrollPane,"Center");
    // Register for mouse events on blankArea and panel.
    blankArea.addMouseMotionListener(this);
    addMouseMotionListener(this);
    setPreferredSize(new Dimension(450, 450));
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
  }
  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() + "\n");
    textArea.setCaretPosition(textArea.getDocument().getLength());
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("MouseMotionEventDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Create and set up the content pane.
    JComponent newContentPane = new MouseMotionEventDemo();
    newContentPane.setOpaque(true); // content panes must be opaque
    frame.setContentPane(newContentPane);
    // Display the window.
    frame.pack();
    frame.setVisible(true);
  }
}
class BlankArea extends JLabel {
  Dimension minSize = new Dimension(100, 100);
  public BlankArea(Color color) {
    setBackground(color);
    setOpaque(true);
    setBorder(BorderFactory.createLineBorder(Color.black));
  }
  public Dimension getMinimumSize() {
    return minSize;
  }
  public Dimension getPreferredSize() {
    return minSize;
  }
}





The MouseMotionListener Interface

This interface defines methods that are called when the mouse is moved or dragged with a button pressed.

Defined MethodsDescriptionmouseMoved(MouseEvent e)Called when the mouse is moved within a componentmouseDragged(MouseEvent e)Called when the mouse is moved within a component while a mouse button is held down