标签:style blog http io ar color os 使用 sp
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41346969
看完这篇文章,你可能会学到到知识如下:
(1)JAVASWING中到鼠标监听常用监听器,包括MouseListener、MouseWheelListener、MouseMotionListener。
(2)不同到鼠标监听器到作用各是什么、鼠标进行什么操作时才调用。
(3)MouseListener和MouseAdapter的区别,最好使用MouseAdapter而不是MouseListener,这样会使得代码比较整洁。
(4)通过实例进一步对这些监听进行理解。
当用户点击鼠标时,会调用三个监听器方法:
A: 鼠标第一次被按下时调用mousePressed,鼠标被释放时调用mouseReleased,最后调用mouseClicked。
B: 用MouseEvent类对象作为参数,调用getX()和getY()方法可以获得鼠标被按下时鼠标指针所在到x和y坐标。
C: 如果要想区分是单击、双击、三击就需要使用getClickCount方法。
需要注意的是:
(1) 如果在移动鼠标的同时按下鼠标,就会调用mouseMoved而不是mouseDragged;只有鼠标在一个组件内部停留时才会调用mouseMoved方法,如果鼠标拖
到组件外面mouseDragged方法也会被调用
(2)mouseEntered和mouseExited方法是在鼠标进入或者移出组件时被调用。
(3)大部分到应用都对鼠标点击感兴趣,而对鼠标移动不感兴趣,但是鼠标移动事件发生到频率又很高,所以鼠标移动事件和拖动事件被单独定义在了
MouseMotionListener这个独立到接口中。
MouseListener VS MouseAdapter VS MouseWheelListener VS MouseMotionListener VS MouseMotionAdapter
(1)在上面的几种监听中,比较常用的是MouseListener和MouseMotionListener,而MouseWheelListener用的比较少。
(2)MouseAdapter扩展了MouseListener,MouseAdapter针对性比较强,当不需要实现MouseListener所有的方法时,可以使用MouseAdapter来来选择需要使
用哪一种或若干种监听进行实现,这样使得代码看起来更加简洁,也减少了代码量。
(3)MouseMotionAdapter也是扩展了MouseMotionListener。
(4)MouseWheelListener是鼠标滚轮监听器,一般不使用该监听。
/**MouseListener接口包含5个鼠标方法**/
button.addMouseListener(new MouseListener(){ @Override public void mouseClicked(MouseEvent e) { /**鼠标按键在组件上单击(按下并释放)时调用。**/ } @Override public void mouseEntered(MouseEvent e) { /**鼠标进入到组件上时调用。**/ } @Override public void mouseExited(MouseEvent e) { /**鼠标离开组件时调用。**/ } @Override public void mousePressed(MouseEvent e) { /**鼠标按键在组件上按下时调用。**/ } @Override public void mouseReleased(MouseEvent e) { /**鼠标按钮在组件上释放时调用。**/ }} );
button.addMouseListener(new MouseAdapter(){ /**这里选择mouseClicked方法 **/ @Override public void mouseClicked(MouseEvent e) { super.mouseClicked(e); } });
button.addMouseMotionListener(new MouseMotionListener(){ @Override public void mouseDragged(MouseEvent e) { /**鼠标按键在组件上按下并拖动时调用。**/ } @Override public void mouseMoved(MouseEvent e) { /**鼠标光标移动到组件上但无按键按下时调用。**/ } });
button.addMouseWheelListener(new MouseWheelListener(){ @Override public void mouseWheelMoved(MouseWheelEvent e) { /**鼠标滚轮旋转时调用。**/ } });
鼠标监听实例如下所示:
import java.awt.Cursor; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.util.ArrayList; import java.util.List; import javax.swing.JComponent; import javax.swing.JFrame; /** * 鼠标监听 * 在创建的画布上创建、擦除、移动方块 * 1.点击鼠标会创建一个方块 * 2.在已有的方块双击会擦除掉方块 * 3.用鼠标拽住方块可以自由拖动 */ public class TestMouseListener { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { MouseFrame frame = new MouseFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } class MouseFrame extends JFrame { public MouseFrame() { setTitle("鼠标监听测试"); setSize(300, 200); MouseComponent component = new MouseComponent(); add(component); } } class MouseComponent extends JComponent { private static final int SIDELENG = 10; private List<Rectangle2D> square; private Rectangle2D current; public MouseComponent() { square = new ArrayList<Rectangle2D>(); current = null; addMouseListener(new MouseHandler()); addMouseMotionListener(new MouseMotionHandler()); } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; for (Rectangle2D r : square) { g2.draw(r); } } public Rectangle2D find(Point2D p) { for (Rectangle2D r : square) { if (r.contains(p)) return r; } return null; } public void add(Point2D p) { double x = p.getX(); double y = p.getY(); current = new Rectangle2D.Double(x - SIDELENG / 2, y - SIDELENG / 2, SIDELENG, SIDELENG); square.add(current); repaint(); } public void remove(Rectangle2D s) { if (s == null) return; if (s == current) current = null; square.remove(s); repaint(); } private class MouseHandler extends MouseAdapter { @Override public void mousePressed(MouseEvent e) { current = find(e.getPoint()); if (current == null) add(e.getPoint()); } @Override public void mouseClicked(MouseEvent e) { current = find(e.getPoint()); if (current != null && e.getClickCount() >= 2) remove(current); } } private class MouseMotionHandler implements MouseMotionListener { @Override public void mouseMoved(MouseEvent e) { if (find(e.getPoint()) == null) setCursor(Cursor.getDefaultCursor()); else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); } @Override public void mouseDragged(MouseEvent e) { if (current != null) { int x = e.getX(); int y = e.getY(); current.setFrame(x - SIDELENG / 2, y - SIDELENG / 2, SIDELENG,SIDELENG); repaint(); } } } }
标签:style blog http io ar color os 使用 sp
原文地址:http://blog.csdn.net/pistolove/article/details/41346969