تشارك هذه المقالة الكود المحدد للتأرجح لتنفيذ السحب وتمديد النموذج للرجوع إليه. المحتوى المحدد كما يلي
عند استخدام setundecorated (صحيح) لإزالة شريط العنوان بواسطة JFrame ، يجب عليك كتابة وظائف السحب والسحب بنفسك.
فيما يلي العروض. لا يمكن لبرنامج لقطة الشاشة الخاص بي التقاط المؤشر باستثناء المؤشر الافتراضي للنظام ، وبالتالي فإن التغييرات في المؤشرات في كل اتجاه لا تنعكس في الشكل.
الرمز كما يلي:
استيراد javax.swing.*؛ استيراد java.awt.*؛ / *** نموذج السحب والتمدد*/ الفئة العامة winResizeEmo {private jframe jf ؛ public winResizeEmo () {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 (صواب) ؛ } public static void main (string [] args) {new winResizeEmo () ؛ }} استيراد javax.swing.*؛ استيراد java.awt.*؛ استيراد java.awt.event.mouseadapter ؛ استيراد java.awt.event.mouseevent ؛ /*** تنفيذ التمدد والسحب في جميع اتجاهات النافذة. */ RESIZEEVENT من الطبقة العامة يمتد mouseadapter {public jframe jf ؛ Private Point 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 static int int drag_right = 6 ؛ خاص ثابت نهائي int drag_bottom = 7 ؛ خاص ثابت نهائي int drag_bottomleft = 8 ؛ int static int drag_bottomright = 9 ؛ RESIZEEVENT (JFRAME JF) {this.jf = jf ؛ } Override public void mousepressed (mouseevent e) {prepos = } Override public void mouseMoved (mouseevent e) {areacheck (e.getPoint ()) ؛ } Override public void mousedragged (mouseevent e) {curpos = jfpos = jf.getLocation () ؛ السحب () ؛ prepos = curpos ؛ } private void dressaction () {switch (dragtype) {case drag_move: jf.setLocation (jfpos.x+curpos.x-prepos.x ، jfpos.y+curpos.y-prepos.y) ؛ استراحة؛ case drag_up: // x يبقى الموضع دون تغيير ، تغيير الموضع ، ويتغير الارتفاع jf.setlocation (jfpos.x ، jfpos.y+curpos.y-prepos.y) ؛ jf.setsize (jf.getWidth () ، jf.getheight ()-(curpos.y-prepos.y)) ؛ استراحة؛ case drag_left: // y يبقى موضع غير متغير ، تغييرات في الموضع ، تغييرات العرض 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 يظل موضع y دون تغيير ، تغيير العرض jf.setLocation (jfpos.x ، jfpos.y) ؛ jf.setsize (jf.getWidth ()+(curpos.x-prepos.x) ، jf.getheight ()) ؛ استراحة؛ case drag_bottom: // x ، y يظل موضع y دون تغيير ، ويتغير الارتفاع jf.setLocation (jfpos.x ، jfpos.y) ؛ jf.setsize (jf.getWidth () ، jf.getheight ()+(curpos.y-prepos.y)) ؛ استراحة؛ case drag_upleft: // x ، y positions change ، h and 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.getheight ()-(curpos.y-prepos.y)) ؛ استراحة؛ case drag_bottomright: // x ، y تظل المواضع دون تغيير ، h و w all change 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)) ؛ استراحة؛ 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 () <= arcerth && p.gety () <= warkth) {dragtype = drag_upleft ؛ jf.setCursor (New Cursor (Cursor.nw_resize_cursor)) ؛ } آخر إذا (p.getx ()> اتساع && p.getx () <(jf.getWidth ()-اتساع) jf.setCursor (New Cursor (Cursor.n_resize_cursor)) ؛ } آخر إذا (p.getx ()> = (jf.getWidth ()-اتساع) jf.setCursor (New Cursor (Cursor.ne_resize_cursor)) ؛ } آخر if (p.getx () <= wardth && p.gety () <(jf.getheight ()-عرض) jf.setCursor (New Cursor (Cursor.w_resize_cursor)) ؛ } آخر إذا (p.getx ()> = (jf.getWidth ()-اتساع) && p.gety () <(jf.getheight ()-اتساع) jf.setCursor (New Cursor (Cursor.e_resize_cursor)) ؛ } if if (p.getx () <= warkth && p.gety ()> = (jf.getheight ()-اتساع)) {dragtype = drag_bottomleft ؛ JF.SetCursor (New Cursor (Cursor.sw_resize_cursor)) ؛ } آخر إذا (p.getx ()> اتساع && p.getx () <(jf.getWidth ()-اتساع) jf.setCursor (New Cursor (Cursor.S_Resize_Cursor)) ؛ } آخر إذا (p.getx ()> = (jf.getWidth ()-اتساع) && p.gety ()> = (jf.getheight ()-اتساع)) {dragtype = drag_bottomright ؛ jf.setCursor (New Cursor (Cursor.se_resize_cursor)) ؛ } آخر {dragtype = drag_move ؛ JF.SetCursor (New Cursor (Cursor.move_Cursor)) ؛ العودة كاذبة } إعادة صواب ؛ }}ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.