欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

基于swing实现窗体拖拽和拉伸

时间:2020-10-22 22:56:42|栏目:JAVA代码|点击:

本文为大家分享了swing实现窗体拖拽和拉伸的具体代码,供大家参考,具体内容如下

当用setUndecorated(true) 后 JFrame去掉标题栏后就得自己写拖拽和拉伸功能了。

下面是效果图,我的截图软件不能够截取除系统默认光标外的光标,所以各个方向光标变化在图中没有体现

代码如下:

import javax.swing.*; 
import java.awt.*; 
 
/** 
 * 窗体拖拽和拉伸 
 */ 
public class winReSizeDemo { 
 private JFrame jf; 
 public winReSizeDemo(){ 
  jf=new JFrame(); 
  jf.setUndecorated(true);//去标边界和标题栏 
  jf.setLocationRelativeTo(null);//窗口置中 
  jf.setSize(400,400); 
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  reSizeEvent dg = new reSizeEvent(jf); 
  /**添加两个监听器**/ 
  jf.addMouseListener(dg); 
  jf.addMouseMotionListener(dg); 
  jf.setVisible(true); 
 } 
 public static void main(String [] args){ 
  new winReSizeDemo(); 
 } 
} 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
/** 
 * 实现窗口各个方向拉伸以及拖动。 
 */ 
public class reSizeEvent extends MouseAdapter{ 
 public JFrame jf; 
 private Point prePos,curPos,jfPos; 
 private static final double BREADTH = 15.0;//边界拉伸范围 
 private int dragType; 
 private static final int DRAG_MOVE = 1; 
 private static final int DRAG_UP = 2; 
 private static final int DRAG_UPLEFT = 3; 
 private static final int DRAG_UPRIGHT = 4; 
 private static final int DRAG_LEFT = 5; 
 private static final int DRAG_RIGHT = 6; 
 private static final int DRAG_BOTTOM = 7; 
 private static final int DRAG_BOTTOMLEFT = 8; 
 private static final int DRAG_BOTTOMRIGHT = 9; 
 public reSizeEvent(JFrame jf){ 
  this.jf = jf; 
 } 
 @Override 
 public void mousePressed(MouseEvent e){ 
  prePos = e.getLocationOnScreen(); 
 } 
 @Override 
 public void mouseMoved(MouseEvent e){ 
  areaCheck(e.getPoint()); 
 } 
 @Override 
 public void mouseDragged(MouseEvent e){ 
  curPos = e.getLocationOnScreen(); 
  jfPos = jf.getLocation(); 
  dragAction(); 
  prePos = curPos; 
 } 
 private void dragAction(){ 
  switch(dragType){ 
   case DRAG_MOVE: 
    jf.setLocation(jfPos.x+curPos.x-prePos.x, 
      jfPos.y+curPos.y-prePos.y); 
    break; 
   case DRAG_UP://x位置不变,y位置变化,并且Height变化 
    jf.setLocation(jfPos.x, 
      jfPos.y+curPos.y-prePos.y); 
    jf.setSize(jf.getWidth(), jf.getHeight()-(curPos.y-prePos.y)); 
    break; 
   case DRAG_LEFT://y位置不变,x位置变化,width变化 
    jf.setLocation(jfPos.x+curPos.x-prePos.x, 
      jfPos.y); 
    jf.setSize(jf.getWidth()-(curPos.x-prePos.x), jf.getHeight()); 
    break; 
   case DRAG_RIGHT://x,y位置不变,width变化 
    jf.setLocation(jfPos.x, 
      jfPos.y); 
    jf.setSize(jf.getWidth()+(curPos.x-prePos.x), jf.getHeight()); 
    break; 
   case DRAG_BOTTOM://x,y位置不变,Height变化 
    jf.setLocation(jfPos.x, 
      jfPos.y); 
    jf.setSize(jf.getWidth(), jf.getHeight()+(curPos.y-prePos.y)); 
    break; 
   case DRAG_UPLEFT://x,y位置均变化,h,w均变化 
    jf.setLocation(jfPos.x+curPos.x-prePos.x, 
      jfPos.y+curPos.y-prePos.y); 
    jf.setSize(jf.getWidth()-(curPos.x-prePos.x), jf.getHeight()-(curPos.y-prePos.y)); 
    break; 
   case DRAG_BOTTOMRIGHT://x,y位置均不变,h,w变化 
    jf.setLocation(jfPos.x, 
      jfPos.y); 
    jf.setSize(jf.getWidth()+(curPos.x-prePos.x), jf.getHeight()+(curPos.y-prePos.y)); 
    break; 
   case DRAG_UPRIGHT://x位置不变,y,w,h变化 
    jf.setLocation(jfPos.x, 
      jfPos.y+curPos.y-prePos.y); 
    jf.setSize(jf.getWidth()+(curPos.x-prePos.x), jf.getHeight()-(curPos.y-prePos.y)); 
    break; 
   case DRAG_BOTTOMLEFT://y不变,xwh变化 
    jf.setLocation(jfPos.x+curPos.x-prePos.x, 
      jfPos.y); 
    jf.setSize(jf.getWidth()-(curPos.x-prePos.x), jf.getHeight()+(curPos.y-prePos.y)); 
    break; 
   default: 
    break; 
  } 
 } 
 private boolean areaCheck(Point p){ 
  if(p.getX()<=BREADTH && p.getY()<=BREADTH){ 
   dragType = DRAG_UPLEFT; 
   jf.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR)); 
  }else if(p.getX()>BREADTH 
    && p.getX()<(jf.getWidth()-BREADTH) 
    && p.getY()<=BREADTH){ 
   dragType = DRAG_UP; 
   jf.setCursor(new Cursor(Cursor.N_RESIZE_CURSOR)); 
  }else if(p.getX()>=(jf.getWidth()-BREADTH) && p.getY()<=BREADTH){ 
   dragType = DRAG_UPRIGHT; 
   jf.setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR)); 
  }else if(p.getX()<=BREADTH 
    && p.getY()<(jf.getHeight()-BREADTH) 
    && p.getY()>BREADTH){ 
   dragType = DRAG_LEFT; 
   jf.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR)); 
  }else if(p.getX()>=(jf.getWidth()-BREADTH) 
    && p.getY()<(jf.getHeight()-BREADTH) 
    && p.getY()>BREADTH){ 
   dragType = DRAG_RIGHT; 
   jf.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR)); 
  }else if(p.getX()<=BREADTH 
    && p.getY()>=(jf.getHeight()-BREADTH)){ 
   dragType = DRAG_BOTTOMLEFT; 
   jf.setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR)); 
  }else if(p.getX()>BREADTH 
    && p.getX()<(jf.getWidth()-BREADTH) 
    && p.getY()>=(jf.getHeight()-BREADTH)){ 
   dragType = DRAG_BOTTOM; 
   jf.setCursor(new Cursor(Cursor.S_RESIZE_CURSOR)); 
  }else if(p.getX()>=(jf.getWidth()-BREADTH) 
    && p.getY()>=(jf.getHeight()-BREADTH)){ 
   dragType = DRAG_BOTTOMRIGHT; 
   jf.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR)); 
  }else{ 
   dragType = DRAG_MOVE; 
   jf.setCursor(new Cursor(Cursor.MOVE_CURSOR)); 
   return false; 
  } 
  return true; 
 } 
} 

上一篇:基于synchronized修饰静态和非静态方法

栏    目:JAVA代码

下一篇:Java中的迭代器和foreach原理

本文标题:基于swing实现窗体拖拽和拉伸

本文地址:http://www.codeinn.net/misctech/14850.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有