/**处理鼠标事件*/import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class A0062 extends JFrame{
public A0062(){

A0062B A62B = new A0062B(); //这个是第2个类创建军对像 

getContentPane().setLayout(new BorderLayout());
getContentPane().add(A62B);
}
public static void main(String[] args){
A0062 CD = new A0062();
CD.setTitle("天天");
CD.setDefaultCloseOpeation(JFrame.EXIT_ON_CLOSE);
CD.setSize(200,200);
CD.setVisible(true);
}
}class A0062B extends MessagePanel implements MouseMotionListener{  //MessagePanel 是信息面板类

public A0062B(String s){ 
super(s);
this.addMouseMotionListener(this);
}

public void mouseMoved(MouseEvent e){

}

public void mousedragged(MouseEvent e){  //事件按下鼠标拖动时
setXCoordinate(e.getX()); // XCoordinate 是信息的X坐标
setYCoordinate(e.getY()); // YCoordinate 是信息的Y坐标
}
}////(MessagePanel 是信息面板类) 我不知道有没有这个类!

解决方案 »

  1.   

    MessagePanel这个类书上有
    但是打上去没有现显有这个MessagePanel类
    我这个代码是有错的!
                                   代码(是拖动鼠标在面板上移动信息!)高手能指点指点!
    谢谢!
      

  2.   

    可以用JTextFiled或者要显示多点就要JTextArea显示你的鼠标移动时的坐标啊,
    不难的。
      

  3.   

    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();
             }
          }
       }
    }
    粘了个鼠标事件的代码,楼主可以好好看看