关于java swing中鼠标/键盘组合键事件的讨论-含代码(1)

帖子链接如下:
http://topic.csdn.net/u/20080604/09/75a62467-1e93-4501-abe9-eeb2b1c12cbb.html

在平常swing开发中,keyevent和mouseevent是不可避免的,如input,move,click等,我想很多人都有这方面的经验,
希望大家能整理和总结一下已有的资源和代码,特别是组合事件的代码,如ctrl+鼠标左键,ctrl+shift+k,ctrl+shift+鼠标button
等等,我准备开10贴,共2000分,总结一下这方面的知识,与大家共享。
一个小小的发帖格式要求:
1,先简单说明代码用途
2,尽量是完整,并且拷贝,可运行的代码,如果有注释,更佳

当然,这里我先抛砖引玉:
1,这个代码是CoreJAVA 7th Edithon中v1/v1ch8/MouseTest代码实例,对鼠标事件测试,进行了完整的举例。小而全,建议学习。

/**
  @version 1.31 2004-05-04
  @author Cay Horstmann
*/

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;

public class MouseTest
{
  public static void main(String[] args)
  {
      MouseFrame frame = new MouseFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
  }
}

/**
  A frame containing a panel for testing mouse operations
*/
class MouseFrame extends JFrame
{
  public MouseFrame()
  {
      setTitle("MouseTest");
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      // add panel to frame

      MousePanel panel = new MousePanel();
      add(panel);
  }

  public static final int DEFAULT_WIDTH = 300;
  public static final int DEFAULT_HEIGHT = 200; 
}

/**
  A panel with mouse operations for adding and removing squares.
*/
class MousePanel extends JPanel
{
  public MousePanel()
  {
      squares = new ArrayList <Rectangle2D>();
      current = null;

      addMouseListener(new MouseHandler());
      addMouseMotionListener(new MouseMotionHandler());
  }

  public void paintComponent(Graphics g)
  {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;

      // draw all squares
      for (Rectangle2D r : squares)
        g2.draw(r);
  }

  /**
      Finds the first square containing a point.
      @param p a point
      @return the first square that contains p
  */
  public Rectangle2D find(Point2D p)
  {
      for (Rectangle2D r : squares)
      {
        if (r.contains(p)) return r;
      }
      return null;
  }

  /**
      Adds a square to the collection.
      @param p the center of the square
  */
  public void add(Point2D p)
  {
      double x = p.getX();
      double y = p.getY();

      current = new Rectangle2D.Double(
        x - SIDELENGTH / 2,
        y - SIDELENGTH / 2,
        SIDELENGTH,
        SIDELENGTH);
      squares.add(current);
      repaint();
  }

  /**
      Removes a square from the collection.
      @param s the square to remove
  */
  public void remove(Rectangle2D s)
  {
      if (s == null) return;
      if (s == current) current = null;
      squares.remove(s);
      repaint();
  }


  private static final int SIDELENGTH = 10;
  private ArrayList <Rectangle2D> squares;
  private Rectangle2D current;
  // the square containing the mouse cursor

  private class MouseHandler extends MouseAdapter
  {
      public void mousePressed(MouseEvent event)
      {
        // add a new square if the cursor isn't inside a square
        current = find(event.getPoint());
        if (current == null)
            add(event.getPoint());
      }

      public void mouseClicked(MouseEvent event)
      {
        // remove the current square if double clicked
        current = find(event.getPoint());
        if (current != null && event.getClickCount() >= 2)
            remove(current);
      }
  }

  private class MouseMotionHandler
      implements MouseMotionListener
  {
      public void mouseMoved(MouseEvent event)
      {
        // set the mouse cursor to cross hairs if it is inside
        // a rectangle

        if (find(event.getPoint()) == null)
            setCursor(Cursor.getDefaultCursor());
        else
            setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
      }

      public void mouseDragged(MouseEvent event)
      {
        if (current != null)
        {
            int x = event.getX();
            int y = event.getY();

            // drag the current rectangle to center it at (x, y)
            current.setFrame(
              x - SIDELENGTH / 2,
              y - SIDELENGTH / 2,
              SIDELENGTH,
              SIDELENGTH);
            repaint();
        }
      }
  }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值