what is the problem?
当然是画的框不对!高手快来!

解决方案 »

  1.   

    在init()方法里加上
    addMouseListener(this);
      

  2.   

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.image.*;
    public class TMouseMotion extends JApplet implements MouseListener
    {
        int x,y;
        int o_x,o_y;
        int flag;
          int s_x;
        int s_y;
        int width;
        int height;
        public void init()
        {
            CustomListener ct=new CustomListener(this);
            this.addMouseMotionListener(ct);
            addMouseListener(this);//You missed this line
        }
        public void update(Graphics g)
        {
            paint(g);
        }
        public void paint(Graphics gdraw)
        {
    BufferedImage bufimg=new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_ARGB);
    Graphics g=bufimg.getGraphics();
            g.setColor(this.getBackground());
            g.fillRect(0,0,getWidth(),getHeight());
           g.setColor(Color.blue);
            g.drawString("drag/move mouse......",5,20);
            g.setColor(Color.red);
            if(flag==1)
            {
                g.drawString("don't move!drag the mouse",5,85);
                g.drawString("cursor coordinates:"+x+","+y,5,95);
            }
            else if(flag==2)
            {
                g.drawString("don't drag!move the mouse",5,85);
                g.drawString("cursor coordinates:"+x+","+y,5,95);
            }
            g.setColor(Color.blue);
            g.drawRect(s_x,s_y,width,height);
            gdraw.drawImage(bufimg,0,0,null);
        }    public void mousePressed(MouseEvent e)
        {
            o_x=e.getX();
            o_y=e.getY();
        }
        public void mouseReleased(MouseEvent e)
        {
            o_x=0;
            o_y=0;
        }
        public void mouseClicked(MouseEvent e)
        {
        }
        public void mouseEntered(MouseEvent e)
        {
        }
        public void mouseExited(MouseEvent e)
        {
        }
    }
    class CustomListener implements MouseMotionListener
    {
        TMouseMotion tm;
        public CustomListener(TMouseMotion tm)
        {
            this.tm=tm;
        }
        public void mouseMoved(MouseEvent e)
        {
            tm.flag=2;
            tm.x=e.getX();
            tm.y=e.getY();
            tm.repaint();
        }
        public void mouseDragged(MouseEvent e)
        {
            tm.flag=1;
            tm.x=e.getX();
            tm.y=e.getY();
            if(tm.x>=tm.o_x && tm.y<=tm.o_y)
            {
                tm.s_x=tm.o_x;
                tm.s_y=tm.y;
                tm.width=tm.x-tm.o_x;
                tm.height=tm.o_y-tm.y;
            }
            if(tm.x>=tm.o_x && tm.y>=tm.o_y)
            {
                tm.s_x=tm.o_x;
                tm.s_y=tm.o_y;
                tm.width=tm.x-tm.o_x;
                tm.height=tm.y-tm.o_y;
            }
            if(tm.x<=tm.o_x && tm.y>=tm.o_y)
            {
                tm.s_x=tm.x;
                tm.s_y=tm.o_y;
                tm.width=tm.o_x-tm.x;
                tm.height=tm.y-tm.o_y;
            }
            if(tm.x<=tm.o_x && tm.y<=tm.o_y)
            {
                tm.s_x=tm.x;
                tm.s_y=tm.y;
                tm.width=tm.o_x-tm.x;
                tm.height=tm.o_y-tm.y;
            }        tm.repaint();    }}
      

  3.   

    在init中少了 
    addMouseListener(this);