English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article shares the specific code for implementing window dragging and stretching in Swing for everyone's reference, and the specific content is as follows
After using setUndecorated(true), when the JFrame removes the title bar, you need to write the dragging and stretching function yourself.
Below is the effect diagram, my screenshot software cannot capture cursors other than the system default cursor, so the change of cursors in all directions is not reflected in the figure
The code is as follows:
import javax.swing.*; import java.awt.*; /** * Window dragging and stretching */ public class winReSizeDemo { private JFrame jf; public winReSizeDemo(){ jf=new JFrame(); jf.setUndecorated(true);//Remove the border and title bar jf.setLocationRelativeTo(null);//Center the window jf.setSize(400,400); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); reSizeEvent dg = new reSizeEvent(jf); /**Add two listeners**/ 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; /** * Implement the stretching and dragging of the window in all directions. */ public class reSizeEvent extends MouseAdapter{ public JFrame jf; private Point prePos, curPos, jfPos; private static final double BREADTH = 15.0;//Bereich der Dehnung 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 Position unverändert, y Position geändert und Höhe geändert 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 Position unverändert, x Position geändert, Breite geändert 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 Position unverändert, Breite geändert 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)); && p.getX() < (jf.getWidth() dragType = DRAG_BOTTOM;-BREADTH) && p.getY() <= BREADTH){ dragType = DRAG_UP; jf.setCursor(new Cursor(Cursor.N_RESIZE_CURSOR)); }-BREADTH) && p.getY() <= BREADTH){ dragType = DRAG_UPRIGHT; jf.setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR)); jf.setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR)); && p.getY() < (jf.getHeight(){-BREADTH) && p.getY() > BREADTH){ dragType = DRAG_LEFT; jf.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR)); }-BREADTH) && p.getY() < (jf.getHeight(){-BREADTH) && p.getY() > BREADTH){ dragType = DRAG_RIGHT; jf.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR)); jf.setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR)); && p.getY() >= (jf.getHeight()-BREADTH) else if (p.getX() > BREADTH } && p.getX() < (jf.getWidth() dragType = DRAG_BOTTOM;-BREADTH) && p.getY() >= (jf.getHeight()-BREADTH) 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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
声明:本文内容来自网络,版权归作者所有。内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未进行人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#oldtoolbag.com(在发邮件时,请将#更换为@进行举报,并提供相关证据。一经查实,本站将立即删除涉嫌侵权内容。)