บทความนี้แชร์รหัสเฉพาะสำหรับการแกว่งเพื่อใช้การลากและยืดแบบฟอร์มสำหรับการอ้างอิงของคุณ เนื้อหาเฉพาะมีดังนี้
เมื่อใช้ setundecorated (จริง) เพื่อลบแถบชื่อเรื่องโดย jframe คุณต้องเขียนฟังก์ชั่นการลากและดึงด้วยตัวเอง
ด้านล่างคือการเรนเดอร์ ซอฟต์แวร์ภาพหน้าจอของฉันไม่สามารถจับเคอร์เซอร์ได้ยกเว้นเคอร์เซอร์เริ่มต้นของระบบดังนั้นการเปลี่ยนแปลงเคอร์เซอร์ในแต่ละทิศทางจะไม่สะท้อนในรูป
รหัสมีดังนี้:
นำเข้า Javax.swing.*; นำเข้า Java.awt.*; / *** ฟอร์มการลากและยืด*/ คลาสสาธารณะ winresizedemo {ส่วนตัว jframe jf; สาธารณะ winresizedemo () {jf = new JFrame (); jf.setundecorated (true); // demark ขอบเขตและแถบชื่อเรื่อง JF.SetLocationRelativeto (null); // ตั้งหน้าต่าง jf.setsize (400,400); jf.setDefaultCloseOperation (jframe.exit_on_close); ResizeEvent DG = ใหม่ ResizeEvent (JF); / ** เพิ่มผู้ฟังสองคน **/ jf.addmouselistener (DG); jf.addmousemotionlistener (DG); jf.setVisible (จริง); } โมฆะคงที่สาธารณะหลัก (สตริง [] args) {ใหม่ winresizedemo (); - นำเข้า Javax.swing.*; นำเข้า Java.awt.*; นำเข้า java.awt.event.mouseadapter; นำเข้า java.awt.event.mousevent; /*** ใช้การยืดและลากในทุกทิศทางของหน้าต่าง */ คลาสสาธารณะ ResizeEvent ขยาย MouseAdapter {สาธารณะ JFrame JF; Private Point Prepos, Curpos, JFPOS; ความกว้างคู่สุดท้ายคงที่สองครั้ง = 15.0; // เส้นขอบยืดช่วง private int dragtype; int คงสุดท้ายส่วนตัว int drag_move = 1; int คงสุดท้ายคงที่ int drag_up = 2; private static final int drag_upleft = 3; private static final int drag_upright = 4; INT สุดท้ายคงที่ int drag_left = 5; int คงสุดท้ายส่วนตัว int drag_right = 6; int คงสุดท้ายส่วนตัว drag_bottom = 7; int คงสุดท้ายส่วนตัว drag_bottomleft = 8; int คงสุดท้ายส่วนตัว drag_bottomright = 9; สาธารณะ resizeEvent (jframe jf) {this.jf = jf; } @Override โมฆะสาธารณะ mousepressed (MouseEvent E) {prepos = e.getLocationOnsCreen (); } @Override โมฆะสาธารณะ mousemoved (MouseEvent E) {areacheck (e.getPoint ()); } @Override โมฆะสาธารณะ mousedragged (MouseEvent E) {curpos = e.getLocationonScreen (); jfpos = jf.getLocation (); dragaction (); prepos = curpos; } โมฆะส่วนตัว 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 ()); หยุดพัก; กรณี drag_bottom: // x, ตำแหน่ง y ยังคงไม่เปลี่ยนแปลง, การเปลี่ยนแปลงความสูง jf.setLocation (jfpos.x, jfpos.y); jf.setsize (jf.getWidth (), jf.getheight ()+(curpos.y-prepos.y)); หยุดพัก; case drag_upleft: // x, ตำแหน่งการเปลี่ยนแปลง, 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 ยังคงไม่เปลี่ยนแปลง, h และ w ทั้งหมดเปลี่ยน jf.setLocation (jfpos.x, jfpos.y); jf.setsize (jf.getWidth ()+(curpos.x-prepos.x), jf.getheight ()+(curpos.y-prepos.y)); หยุดพัก; 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)); หยุดพัก; กรณี 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; }} บูลีนส่วนตัว areacheck (จุด p) {ถ้า (p.getx () <= ความกว้าง && p.gety () <= ความกว้าง) {dragtype = drag_upleft; JF.SetCursor (เคอร์เซอร์ใหม่ (Cursor.nw_resize_cursor)); } อื่นถ้า (p.getx ()> ความกว้าง && p.getx () <(jf.getWidth ()-ความกว้าง) && p.gety () <= ความกว้าง) {dragtype = drag_up; JF.SetCursor (เคอร์เซอร์ใหม่ (Cursor.n_Resize_Cursor)); } อื่นถ้า (p.getx ()> = (jf.getWidth ()-ความกว้าง) && p.gety () <= ความกว้าง) {dragtype = drag_upright; JF.SetCursor (เคอร์เซอร์ใหม่ (Cursor.NE_RESIZE_CURSOR)); } อื่นถ้า (p.getx () <= ความกว้าง && p.gety () <(jf.getheight ()-ความกว้าง) && p.gety ()> ความกว้าง) {dragtype = drag_left; JF.SetCursor (เคอร์เซอร์ใหม่ (Cursor.w_resize_cursor)); } อื่นถ้า (p.getx ()> = (jf.getWidth ()-ความกว้าง) && p.gety () <(jf.getheight ()-ความกว้าง) && p.gety ()> ความกว้าง) {dragtype = drag_right; JF.SetCursor (เคอร์เซอร์ใหม่ (Cursor.e_Resize_Cursor)); } อื่นถ้า (p.getx () <= ความกว้าง && p.gety ()> = (jf.getheight ()-ความกว้าง)) {dragtype = drag_bottomleft; JF.SetCursor (เคอร์เซอร์ใหม่ (Cursor.sw_resize_cursor)); } อื่นถ้า (p.getx ()> ความกว้าง && p.getx () <(jf.getWidth ()-ความกว้าง) && p.gety ()> = (jf.getheight ()-ความกว้าง)) {dragtype = drag_bottom; JF.SetCursor (เคอร์เซอร์ใหม่ (Cursor.S_RESIZE_CURSOR)); } อื่นถ้า (p.getx ()> = (jf.getWidth ()-ความกว้าง) && p.gety ()> = (jf.getheight ()-ความกว้าง)) {dragtype = drag_bottomright; jf.setCursor (เคอร์เซอร์ใหม่ (Cursor.se_resize_cursor)); } else {dragType = drag_move; JF.SetCursor (เคอร์เซอร์ใหม่ (Cursor.move_cursor)); กลับเท็จ; } return true; -ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น