如何实现鼠标推动过过程中的虚线选取框

解决方案 »

  1.   

    import javax.swing.* ;
    import java.awt.* ;
    import java.awt.event.* ;public class testMove extends JFrame 
    {
    int x1 = 0 ;
    int y1 = 0 ;
    int x2 = 0 ;
    int y2 = 0 ;

    boolean flag = false ;
    testMove()
    {
    this.setSize( 400,300 ) ;
    this.addMouseMotionListener( new MouseMotionAdapter()
    {
    public void mouseDragged(MouseEvent e)
    {
    if( flag )
    {
    x2 = e.getX() ;
    y2 = e.getY() ;
    repaint() ;
    }
    }
    }) ;
    this.addMouseListener( new MouseAdapter()
    {
    public void mousePressed(MouseEvent e)
    {
    if( !flag )
    {
    flag = true ;
    x1 = e.getX() ;
    y1 = e.getY() ;
    }
    }

    public void mouseReleased(MouseEvent e)
    {
    flag = false ;
    }
    }) ;

    this.setVisible( true ) ;
    }

    public void paint( Graphics g)
    {
    super.paint( g ) ;
    Graphics2D g2 = (Graphics2D) g;   
    BasicStroke dashed = new BasicStroke(1.0f,     
    BasicStroke.CAP_BUTT,     
    BasicStroke.JOIN_MITER,     
    10.0f,     
    new float[]{10.0f,5.0f},   
    0.0f);   
    g2.setStroke(dashed);   
    g.drawRect( this.x1, this.y1, this.x2-this.x1, this.y2-this.y1 ) ;
    }

    public static void main( String args[] )
    {
    new testMove() ;
    }
    }是不是这个意思?没判断鼠标移动方向
      

  2.   

    小弟领教了。小弟才学GUI不是很懂。谢谢