欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-16 15:34:46
本文为大家分享了swing实现窗体拖拽和拉伸的具体代码,供大家参考,具体内容如下 当用setundecorated(true) 后 jframe去掉标题栏后就得自己写拖拽...

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

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

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

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

代码如下:

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; 
 } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: