问题已在代码中用红色标出
/**
 * @version 1.20 25 Mar 1998
 * @author Cay Horstmann
 */import java.awt.*;
import java.awt.event.*;
import javax.swing.*;class MousePanel extends JPanel
   implements MouseMotionListener
{  public MousePanel()
   {  addMouseListener(new MouseAdapter() 
         {  public void mousePressed(MouseEvent evt) 
            {  int x = evt.getX();
               int y = evt.getY();
               //如果返回的不是-1则说是被选中一个矩形,而不是进行绘图
               //也就不会去执行下的add(x,y)方法
               current = find(x, y);
               if (current < 0) // not inside a square
                  add(x, y);
            }            public void mouseClicked(MouseEvent evt) 
            {  int x = evt.getX();
               int y = evt.getY();               if (evt.getClickCount() >= 2)
               {  remove(current);
               }
            }
         });
   //鼠标移动侦听器
      addMouseMotionListener(this);
   }   public void paintComponent(Graphics g)
   {  //调试时要一点应用程序就会调用这里
   super.paintComponent(g);
      for (int i = 0; i < nsquares; i++)
         draw(g, i);
   }
   
   public int find(int x, int y)
   {  for (int i = 0; i < nsquares; i++)
   //当第二次的鼠标与上一次鼠标重合时下面的if语句会成立
         if (squares[i].x - SQUARELENGTH / 2 <= x && 
               x <= squares[i].x + SQUARELENGTH / 2 
               && squares[i].y - SQUARELENGTH / 2 <= y 
               && y <= squares[i].y + SQUARELENGTH / 2)
          //if中的条件也就是用鼠标选中哪个矩形的条件
          //返回的i是被选中的那个矩形
            return i;
      return -1;
   }   public void draw(Graphics g, int i)
   {  //这里- SQUARELENGTH / 2的原因是:让鼠标指针放在矩形的中心,
   //如果- SQUARELENGTH / 2鼠标指针会放在矩形的左上角
   //squares[i].x是鼠标的在面板上的x轴
   g.drawRect(squares[i].x - SQUARELENGTH / 2, 
         squares[i].y - SQUARELENGTH / 2, 
         SQUARELENGTH, 
         SQUARELENGTH);
   }   public void add(int x, int y)
   {  if (nsquares < MAXNSQUARES)
   //把这次的坐标点存入点对象数组中
      {  squares[nsquares] = new Point(x, y);
         current = nsquares;
         nsquares++;
         repaint();
      }
   }   public void remove(int n)
   { 
   //在什么条件下n<0,n>=nsquares会成立
   //这里的nsquares应该是n的下一个
   if (n < 0 || n >= nsquares) return;      nsquares--;
      //这是重新定义数组,把刚才选择的那个矩形从数组中去掉
      squares[n] = squares[nsquares];
      //为什么还要把curren置为-1
      if (current == n) current = -1;
      repaint();
   }   public void mouseMoved(MouseEvent evt) 
   {  int x = evt.getX();
      int y = evt.getY();      if (find(x, y) >= 0) 
       //setCursor()设置光标
       //得到具有有指定预定义类型的光标对象
       //Cursor.CROSSHAIR_CURSOR返回十字光标类型
         setCursor(Cursor.getPredefinedCursor
            (Cursor.CROSSHAIR_CURSOR)); 
      else 
       //返回系统默认的光标
         setCursor(Cursor.getDefaultCursor());
   }   public void mouseDragged(MouseEvent evt) 
   {  int x = evt.getX();
      int y = evt.getY();      if (current >= 0)
      {  Graphics g = getGraphics();
         g.setXORMode(getBackground());
         draw(g, current);      
         squares[current].x = x;
         squares[current].y = y;
         draw(g, current);
         g.dispose();
      }
   }   private static final int SQUARELENGTH = 10;
   private static final int MAXNSQUARES = 100;
   private Point[] squares = new Point[MAXNSQUARES];
   private int nsquares = 0;
   private int current = -1;
}class MouseFrame extends JFrame
{  public MouseFrame()
   {  setTitle("MouseTest");
      setSize(300, 200);
      addWindowListener(new WindowAdapter()
         {  public void windowClosing(WindowEvent e)
            {  System.exit(0);
            }
         } );      Container contentPane = getContentPane();
      contentPane.add(new MousePanel());
   }
}public class MouseTest
{  public static void main(String[] args)
   {  JFrame frame = new MouseFrame();
      frame.show();  
   }
}