注释掉下面的repaint()试试看。class SelfContainedPanel extends JPanel {
   private int x1, y1, x2, y2;
   public SelfContainedPanel()
   {
      addMouseListener(
         new MouseAdapter() {
            public void mousePressed( MouseEvent e )
            {
               x1 = e.getX();
               y1 = e.getY();
            }            public void mouseReleased( MouseEvent e )
            {
               x2 = e.getX();
               y2 = e.getY();
               //repaint();
            }
           
         }
      );      addMouseMotionListener(
         new MouseMotionAdapter() {
            public void mouseDragged( MouseEvent e )
            {
               setCursor(Cursor.getPredefinedCursor   (Cursor.CROSSHAIR_CURSOR));
               x2 = e.getX();
               y2 = e.getY();
               //repaint();
            }
          }
      );
   }

解决方案 »

  1.   

    要用个容器,arrarys什么的,把以前画过的直线寸起来,最后画的时候,要把arrarys里面的直线全部重画。应该行的通
      

  2.   

    很简单,就是说你必须用一个容器来保存你所画过的线,然后在paint方法中把这些线都重新画就可以了.
      

  3.   

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;public class Test extends Frame
    {
       public static void main(String[] args)
       {
          new Test();
       }
       public Test()
       {
          this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){dispose();System.exit(0);}});
          this.setSize(300,300);
          this.setLocation(300,0);
          this.add(new Draw());
          this.show();
       }
       protected void processWindowEvent(WindowEvent event)
       {
          super.processWindowEvent(event);
          if(event.getID() == WindowEvent.WINDOW_CLOSING)
          {
             System.exit(0);
          }
       }
    }
    class Draw extends Canvas
    {
       int x1=-1;
       int y1=-1;
       int x2=-1;
       int y2=-1;
       public Draw()
       {
          this.setBackground(Color.white);
          this.addMouseListener(new MyMouseListener(this));
          this.addMouseMotionListener(new MyMouseMotionListener(this));
       }
       public void paint(Graphics g)
       {
          if(x1==-1 || y1==-1)
          {
             x1=x2;
             y1=y2;
          }
          g.drawLine(x1,y1,x2,y2);
          x1=x2;
          y1=y2;
       }
       public void update(Graphics g)
       {
          this.paint(g);
       }
    }
    class MyMouseListener implements MouseListener
    {
       Draw draw;
       public MyMouseListener(Draw draw)
       {
          this.draw=draw;
       }
       public void mouseClicked(MouseEvent e)
       {
          //System.out.println("mouseClicked");
       }
       public void mouseEntered(MouseEvent e)
       {
          //System.out.println("mouseEntered");
       }
       public void mouseExited(MouseEvent e)
       {
          //System.out.println("mouseExited");
       }
       public void mousePressed(MouseEvent e)
       {
          //System.out.println("mousePressed");
          draw.x1=-1;
          draw.y1=-1;
       }
       public void mouseReleased(MouseEvent e)
       {
          //System.out.println("mouseReleased");
          draw.x1=draw.x2;
          draw.y1=draw.y2;
       }
    }
    class MyMouseMotionListener implements MouseMotionListener
    {
       Draw draw;
       public MyMouseMotionListener(Draw draw)
       {
          this.draw=draw;
       }
       public void mouseDragged(MouseEvent e)
       {
          //System.out.println("mouseDragged");
          draw.x2=e.getX();
          draw.y2=e.getY();
          draw.repaint();
       }
       public void mouseMoved(MouseEvent e)
       {
          //System.out.println("mouseMoved");
       }
    }
      

  4.   

    你的那个Test程序再最小化之后,
    那些图象就没有了。
      

  5.   


        private int[] x1 = new int[ 0 ];
      private int[] y1 = new int[ 0 ];
      private int[] x2 = new int[ 0 ];
      private int[] y2 = new int[ 0 ];
      boolean isNotDrag = true;
      public Untitled1() {
          addMouseListener(
              new MouseAdapter() {
              public void mousePressed( MouseEvent e ) {
                  x1 = addElement( e.getX(), x1 );
                  y1 = addElement( e.getY(), y1 );
              }          public void mouseReleased( MouseEvent e ) {
                  isNotDrag = true;
                  x2[ x2.length - 1 ] = e.getX();
                  y2[ y2.length - 1 ] = e.getY();
                  repaint();
              }      }
          );      addMouseMotionListener(
              new MouseMotionAdapter() {
              public void mouseDragged( MouseEvent e ) {
                  setCursor( Cursor.getPredefinedCursor( Cursor.CROSSHAIR_CURSOR ) );
                  if( isNotDrag ){
                      x2 = addElement( e.getX(), x2 );
                      y2 = addElement( e.getY(), y2 );
                      isNotDrag = false;
                      repaint();
                  }else{
                      x2[ x2.length - 1 ] = e.getX();
                      y2[ y2.length - 1 ] = e.getY();
                      repaint();
                  }
              }
          }
          );
      }  public Dimension getPreferredSize() {
          return new Dimension( 150, 100 );
      }  public void paintComponent( Graphics g ) {
          super.paintComponent( g );
          for( int i = 0; i < x2.length; i++ ){
              g.drawLine( x1[ i ], y1[ i ], x2[ i ], y2[ i ] );
          }
      }  public int[] addElement( int n, int[] na ) {
          int[] temp = new int[ na.length + 1 ];
          for( int i = 0; i < na.length; i++ ){
              temp[ i ] = na[ i ];
          }
          temp[ na.length ] = n;
          return temp;
      }