Este artigo compartilha o código específico do Swing para implementar o arrasto e o alongamento do formulário para sua referência. O conteúdo específico é o seguinte
Ao usar o setundeCorated (true) para remover a barra de título do JFrame, você deve escrever as funções de arrastar e puxar sozinho.
Abaixo estão as representações. Meu software de captura de tela não pode capturar o cursor, exceto o cursor padrão do sistema; portanto, as alterações nos cursores em cada direção não se refletem na figura.
O código é o seguinte:
importar javax.swing.*; importar java.awt.*; / *** formulário arrasto e alongamento*/ public classe winResizedemo {private jframe jf; public winResizedemo () {jf = new jframe (); jf.setUNDecorated (true); // DeMark a barra de limite e título jf.setLocationRelativeto (null); // Defina a janela jf.setsize (400.400); jf.setDefaultCloseoperation (jframe.exit_on_close); REDRIEDEVENT DG = novo REDRIEDEVENT (JF); / ** Adicione dois ouvintes **/ jf.addmouselistener (DG); jf.addmousemotionListener (DG); jf.setVisible (true); } public static void main (string [] args) {new winResizedemo (); }} importar javax.swing.*; importar java.awt.*; importar java.awt.event.mouseAdapter; importar java.awt.event.mouseevent; /*** Implemente o alongamento e o arrasto em todas as direções da janela. */ classe pública REDRIEDEVENT estende mouseAdapter {public jframe jf; Private Point Prepos, Curpos, JFPOs; Private estático final duplo largura = 15,0; // alcance de fronteira Private Int Dragtype; private estático final int drag_move = 1; private estático final int drag_up = 2; PRIVADO ESTÁTICO PRIVIVO Int Drag_upleft = 3; private estático final Int Drag_UPRIGHT = 4; private estático final int drag_left = 5; private estático final int drag_right = 6; private estático final int drag_bottom = 7; private estático final int drag_bottomleft = 8; private estático final int drag_bottomright = 9; public REDRIEDEVENT (JFRAME JF) {this.jf = jf; } @Override public void MousePressed (mouseevent e) {prepopas = e.getLocationOnscreen (); } @Override public void mouseMoved (mouseevent e) {areacheck (e.getPoint ()); } @Override public void mousedragged (mouseevent e) {curpas = e.getLocationOnscreen (); jfpos = jf.getLocation (); dragaction (); prepopas = curpas; } private void dragaction () {switch (dragtype) {case drag_move: jf.setLocation (jfpos.x+curpos.x -prepos.x, jfpos.y+curpas.y -prepos.y); quebrar; Case drag_up: // A posição X permanece inalterada, as mudanças de posição y e alterações de altura jf.setLocation (jfpos.x, jfpos.y+curpas.y-prepos.y); jf.setsize (jf.getwidth (), jf.getheight ()-(curpos.y-prepos.y)); quebrar; Case drag_left: // A posição Y permanece inalterada, as alterações da posição x, alterações de largura jf.setLocation (jfpos.x+curpos.x -perpos.x, jfpos.y); jf.setsize (jf.getwidth ()-(curpos.x -prepos.x), jf.getHeight ()); quebrar; case drag_right: // x, y A posição permanece inalterada, alterações de largura jf.setLocation (jfpos.x, jfpos.y); jf.setsize (jf.getwidth ()+(curpas.x -prepos.x), jf.getHeight ()); quebrar; case drag_bottom: // x, y A posição permanece inalterada, alterações de altura jf.setLocation (jfpos.x, jfpos.y); jf.setsize (jf.getwidth (), jf.getheight ()+(curpas.y-prepos.y)); quebrar; case drag_upleft: // x, y posições mudam, h e w All Change jf.setLocation (jfpos.x+curpos.x -prepos.x, jfpos.y+curpos.y -prepos.y); jf.setsize (jf.getwidth ()-(curpos.x -prepos.x), jf.gethight ()-(curpos.y-prepos.y)); quebrar; case drag_bottomright: // x, y posições permanecem inalteradas, h e w altere jf.setLocation (jfpos.x, jfpos.y); jf.setsize (jf.getwidth ()+(curpos.x -prepos.x), jf.gethight ()+(curpos.y -prepos.y)); quebrar; case drag_uPright: // x A posição permanece inalterada, y, w, h altera jf.setLocation (jfpos.x, jfpos.y+curpas.y -prepos.y); jf.setsize (jf.getwidth ()+(curpos.x -prepos.x), jf.gethight ()-(curpos.y -perpos.y)); quebrar; case drag_bottomleft: // y permanece inalterado, o XWH altera jf.setLocation (jfpos.x+curpos.x -perpos.x, jfpos.y); jf.setsize (jf.getwidth ()-(curpos.x -prepos.x), jf.gethight ()+(curpas.y -perpos.y)); quebrar; Padrão: quebra; }} private boolean areacheck (ponto p) {if (p.getx () <= larth && p.gety () <= largura) {dragtype = drag_upleft; jf.setCursor (New Cursor (cursor.nw_resize_cursor)); } else if (p.getx ()> largura && p.getx () <(jf.getwidth ()-largura) && p.gety () <= largura) {dragtype = drag_up; jf.setCursor (New Cursor (cursor.n_resize_cursor)); } else if (p.getx ()> = (jf.getwidth ()-largura) && p.gety () <= largura) {dragtype = drag_uPright; jf.setCursor (New Cursor (cursor.ne_resize_cursor)); } else if (p.getx () <= largura && p.gety () <(jf.getHeight ()-largura) && p.gety ()> largura) {dragtype = drag_left; jf.setCursor (New Cursor (cursor.w_resize_cursor)); } else if (p.getx ()> = (jf.getwidth ()-largura) && p.gety () <(jf.getheight ()-largura) && p.gety ()> largura) {dragtype = drag_right; jf.setCursor (New Cursor (cursor.e_resize_cursor)); } else if (p.getx () <= largura && p.gety ()> = (jf.getHeight ()-largura)) {dragtype = drag_bottomleft; jf.setCursor (New Cursor (cursor.sw_resize_cursor)); } else if (p.getx ()> largura && p.getx () <(jf.getwidth ()-largura) && p.gety ()> = (jf.getHeight ()-largura)) {dragtype = drag_bottom; jf.setCursor (New Cursor (cursor.s_resize_cursor)); } else if (p.getx ()> = (jf.getwidth ()-largura) && p.gety ()> = (jf.getheight ()-largura)) {dragtype = drag_bottomright; jf.setCursor (New Cursor (cursor.se_resize_cursor)); } else {dragtype = drag_move; jf.setCursor (New Cursor (cursor.move_cursor)); retornar falso; } retornar true; }}O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.