이 기사에서는 스윙의 특정 코드를 공유하여 참조를 위해 양식의 드래그 및 스트레칭을 구현합니다. 특정 내용은 다음과 같습니다
JFrame으로 타이틀 막대를 제거하기 위해 setUndEcorated (true)를 사용하는 경우 직접 드래그 앤 풀 기능을 작성해야합니다.
아래는 렌더링입니다. 내 스크린 샷 소프트웨어는 시스템의 기본 커서를 제외하고는 커서를 캡처 할 수 없으므로 각 방향의 커서의 변경 사항이 그림에 반영되지 않습니다.
코드는 다음과 같습니다.
javax.swing을 가져옵니다.*; java.awt.*; / *** 형태 드래그 앤 스트레치*/ public class winresizedemo {private jframe jf; public winresizedemo () {jf = new Jframe (); JF.SetUndEcorated (true); // 경계 및 제목 바 JF.SetLocationRelativeTo (NULL); // Window jf.SetSize (400,400)를 설정합니다. JF.SetDefaultCloseOperation (jframe.exit_on_close); resizeevent dg = 새로운 resizeevent (jf); / ** 두 청취자 추가 **/ jf.addmouselistener (dg); jf.addmousemotionListener (dg); jf.set -Visible (true); } public static void main (String [] args) {new WinResizedEmo (); }} javax.swing을 가져옵니다.*; java.awt.*; import java.awt.event.mouseadapter; java.awt.event.mouseevent import; /*** 창의 모든 방향에서 스트레칭 및 드래그를 구현합니다. */ public class resizeevent 확장 MouseAdapter {public jframe jf; 개인 포인트 Prepos, Curpos, JFPOS; 개인 정적 최종 이중 폭이 15.0; // 경계 스트레치 범위 개인 int dragtype; 개인 정적 최종 최종 int drag_move = 1; 개인 정적 최종 최종 int drag_up = 2; 개인 정적 최종 최종 int drag_upleft = 3; 개인 정적 최종 최종 int drag_upright = 4; 개인 정적 최종 최종 int drag_left = 5; 개인 정적 최종 최종 int drag_right = 6; 개인 정적 최종 최종 int drag_bottom = 7; 개인 정적 최종 최종 int drag_bottomleft = 8; 개인 정적 최종 최종 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 (); 드래그 기능 (); prepos = curpos; } private void dragaction () {switch (dragtype) {case drag_move : jf.setlocation (jfpos.x+curpos.x-prepos.x, jfpos.y+curpos.y-prepos.y); 부서지다; Case Drag_up : // x 위치는 변경되지 않고 y 위치가 변경되며 높이가 jf.setlocation (jfpos.x, jfpos.y+curpos.y-prepos.y); jf.setsize (jf.getwidth (), jf.getheight ()-(curpos.y-prepos.y)); 부서지다; Case Drag_Left : // y 위치는 변경되지 않은 상태로 유지되며 X 위치 변경, 너비는 jf.setLocation (jfpos.x+curpos.x-prepos.x, jfpos.y); jf.setsize (jf.getwidth ()-(curpos.x-prepos.x), jf.getheight ()); 부서지다; Case Drag_Right : // x, y 위치는 변경되지 않으며 너비는 JF.SetLocation (jfpos.x, jfpos.y); jf.setsize (jf.getwidth ()+(curpos.x-prepos.x), jf.getheight ()); 부서지다; Case Drag_Bottom : // x, y 위치는 변경되지 않은 상태로 유지되며 높이는 JF.SetLocation (jfpos.x, jfpos.y); jf.setsize (jf.getwidth (), jf.getheight ()+(curpos.y-prepos.y)); 부서지다; 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)); 부서지다; Case Drag_Bottomright : // x, y 위치는 변경되지 않은 상태로 유지됩니다. jf.setsize (jf.getwidth ()+(curpos.x-prepos.x), jf.getheight ()+(curpos.y-prepos.y)); 부서지다; Case Drag_Upright : // x 위치는 변경되지 않은 상태로 유지됩니다. jf.setsize (jf.getwidth ()+(curpos.x-prepos.x), jf.getheight ()-(curpos.y-prepos.y)); 부서지다; 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)); 부서지다; 기본값 : 브레이크; }} private boolean areacheck (point p) {if (p.getx () <= wadth && p.gety () <= wadth) {dragtype = drag_upleft; jf.setCursor (new Cursor (cursor.nw_resize_cursor)); } else if (p.getx ()> vsth && p.getx () <(jf.getwidth ()-vroad) && p.gety () <= vidth) {dragtype = drag_up; jf.setCursor (new Cursor (cursor.n_resize_cursor)); } else if (p.getx ()> = (jf.getWidth ()-폭 jf.setCursor (new Cursor (cursor.ne_resize_cursor)); } else if (p.getx () <= wadth && p.gety () <(jf.getheight ()-wadth) && p.gety ()> wadth) {dragtype = drag_left; jf.setCursor (new Cursor (cursor.w_resize_cursor)); } else if (p.getx ()> = (jf.getWidth ()-폭 jf.setCursor (new Cursor (cursor.e_resize_cursor)); } else if (p.getx () <= wadth && p.gety ()> = (jf.getheight ()-wadth)) {dragtype = drag_bottomleft; jf.setCursor (new Cursor (cursor.sw_resize_cursor)); } else if (p.getx ()> wadth && p.getx () <(jf.getwidth ()-vroad) && p.gety ()> = (jf.getheight ()-vrothth)) {dragtype = drag_bottom; jf.setCursor (new Cursor (cursor.s_resize_cursor)); } else if (p.getx ()> = (jf.getWidth ()-폭 jf.setCursor (new Cursor (cursor.se_resize_cursor)); } else {dragtype = drag_move; jf.setCursor (새 커서 (cursor.move_cursor)); 거짓을 반환합니다. } true를 반환합니다. }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.