Dieser Artikel teilt den spezifischen Code für Swing zur Implementierung von Drag & Dehnen von Form für Ihre Referenz. Der spezifische Inhalt ist wie folgt
Bei Verwendung von SetCorated (True), um die Titelleiste von JFRame zu entfernen, müssen Sie die Drag & Pull -Funktionen selbst schreiben.
Unten finden Sie die Renderings. Meine Screenshot -Software kann den Cursor mit Ausnahme des Standard -Cursors des Systems nicht erfassen, sodass sich die Änderungen in Cursors in jeder Richtung nicht in der Abbildung widerspiegeln.
Der Code ist wie folgt:
Javax.swing importieren.*; import Java.awt.*; / *** Form Drag and Stretch*/ Public Class Winresizedemo {private jframe jf; public winresizesiemo () {jf = new Jframe (); jf.setundErdEcorated (true); // Demark die Grenze und die Titelleiste jf.setLocationRelativeto (null); // Setzen Sie das Fenster Jf.SetSize (400.400); jf.setDefaultCloseOperation (jframe.exit_on_close); resizeEvent dg = neuer resizeeEvent (JF); / ** Fügen Sie zwei Hörer hinzu **/ jf.addmouselistener (DG); Jf.AddMousemotionListener (DG); jf.setvisible (true); } public static void main (String [] args) {new Winsesizedemo (); }} Javax.swing importieren.*; import Java.awt.*; Import Java.awt.event.Mouseadapter; import Java.awt.event.mouseeEvent; /*** Implementieren Sie Stretching und Ziehen in alle Richtungen des Fensters. */ public class resizeevent erweitert mouseadapter {public JFrame jf; Private Point Prepos, Curpos, JFPOS; private statische endgültige Doppelbreite = 15,0; // Border Dehnungsbereich Private int DragType; private statische endgültige int drag_move = 1; private statische endgültige int drag_up = 2; private statische endgültige int drag_upleft = 3; private statische endgültige int drag_upright = 4; private statische endgültige int drag_left = 5; private statische endgültige int drag_right = 6; private statische endgültige int drag_bottom = 7; private statische endgültige int drag_bottomleft = 8; private statische endgültige int drag_bottomright = 9; public resizeevent (jframe jf) {this.jf = jf; } @Override public void mousePressed (MouseEvent e) {prepos = e.getLocationOnscreen (); } @Override public void mousemoved (MouseeEvent e) {Areeacheck (e.getPoint ()); } @Override public void mouedragged (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); brechen; case drag_up: // x Position bleibt unverändert, y Positionänderungen und Höhenänderungen JF.SetLocation (jfpos.x, jfpos.y+curpos.y-prepos.y); Jf.SetSize (jf.getwidth (), jf.getheight ()-(curfos.y-prepos.y)); brechen; case Drag_Left: // y Position bleibt unverändert, x Positionsänderungen, Breitenänderungen jf.setLocation (jfpos.x+curpos.x-prepos.x, jfpos.y); Jf.SetSize (jf.getwidth ()-(curpos.x-prepos.x), jf.getheight ()); brechen; case drag_right: // x, y Position bleibt unverändert, width ändert sich jf.setLocation (jfpos.x, jfpos.y); Jf.SetSize (jf.getwidth ()+(curpos.x-prepos.x), jf.getheight ()); brechen; case drag_bottom: // x, y Position bleibt unverändert, Höhenänderungen jf.setLocation (jfpos.x, jfpos.y); Jf.SetSize (jf.getwidth (), jf.getheight ()+(curfos.y-prepos.y)); brechen; case Drag_upleft: // x, y Positionen ändern sich, H und W ändern alle 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)); brechen; case Drag_bottomright: // x, y -Positionen bleiben unverändert, H und W ändern alle jf.setLocation (jfpos.x, jfpos.y); Jf.SetSize (jf.getwidth ()+(curpos.x-prepos.x), jf.getheight ()+(curpos.y-prepos.y)); brechen; case drag_upright: // x Position bleibt unverändert, y, w, h ändert jf.setLocation (jfpos.x, jfpos.y+curpos.y-prepos.y); Jf.SetSize (jf.getwidth ()+(curpos.x-prepos.x), jf.getheight ()-(curpos.y-prepos.y)); brechen; case drag_bottomleft: // y bleibt unverändert, ux ändert sich jf.setLocation (jfpos.x+curpos.x-prepos.x, jfpos.y); Jf.SetSize (jf.getwidth ()-(curpos.x-prepos.x), jf.getheight ()+(curpos.y-prepos.y)); brechen; Standard: Break; }} private boolean arereacheck (Punkt p) {if (p.getX () <= breit && p.gety () <= breit) {DragType = Drag_Upleft; jf.setCursor (neuer Cursor (cursor.nw_resize_cursor)); } else if (p.getX ()> breit && p.getX () <(jf.getwidth ()-Breite) && P.Gety () <= Breite) {DragType = Drag_Up; jf.setCursor (neuer Cursor (cursor.n_resize_cursor)); } else if (p.getX ()> = (jf.getWidth ()-Breite) && P.Gety () <= Breite) {DragType = Drag_Upright; JF.SetCursor (neuer Cursor (Cursor.ne_Resize_Cursor)); } else if (p.getX () <= breit && p.gety () <(jf.getheight ()-Breite) && p.gety ()> breit) {DragType = Drag_Left; jf.setCursor (neuer Cursor (cursor.w_resize_cursor)); } else if (p.getX ()> = (jf.getWidth ()-Breite) && P.Gety () <(jf.getheight ()-Breite) && P.Gety ()> Breite) {Dragtype = Drag_Right; jf.setCursor (neuer Cursor (cursor.e_resize_cursor)); } else if (p.getX () <= breit && p.gety ()> = (jf.getheight ()-Breite)) {Dragtype = Drag_bottomleft; JF.SetCursor (neuer Cursor (Cursor.sw_Resize_Cursor)); } else if (p.getX ()> breit && p.getX () <(jf.getWidth ()-Breite) && p.gety ()> = (jf.getheight ()-Breite)) {Dragtype = Drag_bottom; jf.setCursor (neuer Cursor (cursor.s_resize_cursor)); } else if (p.getX ()> = (jf.getWidth ()-Breite) && P.Gety ()> = (jf.getheight ()-Breite)) {DragType = Drag_bottomright; jf.setCursor (neuer Cursor (Cursor.se_Resize_Cursor)); } else {DragType = Drag_Move; JF.SetCursor (neuer Cursor (Cursor.Move_Cursor)); false zurückgeben; } Return true; }}Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.