この記事では、スイングの特定のコードを共有して、リファレンスのためにフォームのドラッグとストレッチングを実装します。特定のコンテンツは次のとおりです
setundecorated(true)を使用してJFrameでタイトルバーを削除する場合、ドラッグアンドプル機能を自分で作成する必要があります。
以下はレンダリングです。私のスクリーンショットソフトウェアは、システムのデフォルトカーソルを除いてカーソルをキャプチャできないため、各方向のカーソルの変更は図に反映されません。
コードは次のとおりです。
javax.swing。*; java.awt。*; / ***フォームドラッグアンドストレッチ*/パブリッククラスWinResizedEMO {private jframe JF; public winresizemo(){jf = new JFrame(); jf.setundecorated(true); //境界とタイトルバーJf.setlocationRelativeto(null); //ウィンドウを設定jf.setsize(400,400); jf.setDefaultCloseoperation(jframe.exit_on_close); resizeevent dg = new Resizeevent(JF); / ** 2人のリスナーを追加**/ jf.addmouseListener(dg); jf.addmousemotionListener(dg); jf.setVisible(true); } public static void main(string [] args){new winresizedemo(); }} javax.swing。*; java.awt。*; java.awt.event.mouseadapterをインポートします。 java.awt.event.MouseEventをインポートします。 /***ウィンドウのすべての方向にストレッチとドラッグを実装します。 */ public class resizeeventは、Mouseadapterを拡張します{public jframe jf;プライベートポイントプレポス、curpos、jfpos;プライベート静的最終二重幅= 15.0; //ボーダーストレッチレンジプライベートINTドラッグタイプ。プライベート静的最終int drag_move = 1;プライベート静的最終int drag_up = 2;プライベート静的最終int drag_upleft = 3;プライベート静的最終int drag_upright = 4;プライベート静的最終int drag_left = 5;プライベート静的最終int drag_right = 6;プライベート静的最終int drag_bottom = 7;プライベート静的最終int drag_bottomleft = 8;プライベート静的最終int drag_bottomright = 9; public Resizeevent(jframe jf){this.jf = jf; } @Override public void mousepressed(mousevent e){prepos = e.getLocationOnscreen(); } @Override public void mousemoved(mousevent 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);壊す;ケースdrag_up:// x位置は変更されず、y位置の変更、および高さの変更jf.setlocation(jfpos.x、jfpos.y+curpos.y-prepos.y); jf.setsize(jf.getWidth()、jf.getheight() - (curpos.y-prepos.y));壊す;ケースdrag_left:// y位置は変更されません、x位置の変更、幅の変更jf.setlocation(jfpos.x+curpos.x-prepos.x、jfpos.y); jf.setsize(jf.getWidth() - (curpos.x-prepos.x)、jf.getheigh());壊す; case drag_right:// x、y位置は変更されません、幅の変更jf.setlocation(jfpos.x、jfpos.y); jf.setsize(jf.getWidth()+(curpos.x-prepos.x)、jf.getheigh());壊す;ケースdrag_bottom:// x、y位置は変更されず、高さの変化jf.setlocation(jfpos.x、jfpos.y); jf.setsize(jf.getWidth()、jf.getheight()+(curpos.y-prepos.y));壊す;ケース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));壊す; 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));壊す;ケース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));壊す;デフォルト:break; }} private boolean areacheck(point p){if(p.getx()<= bredth && p.gety()<= bredth){dragtype = drag_upleft; jf.setcursor(new cursor(cursor.nw_resize_cursor)); } else if(p.getx()> swerdth && p.getx()<(jf.getWidth() - bredth)&& p.gety()<= bredth){dragtype = drag_up; jf.setcursor(new cursor(cursor.n_resize_cursor)); } else if(p.getx()> =(jf.getwidth() - bredth)&& p.gety()<= bredth){dragtype = drag_upright; jf.setcursor(new cursor(cursor.ne_resize_cursor)); } else if(p.getx()<= bredth && p.gety()<(jf.getheight() - breadth)&& p.gety()> swardth){dragtype = drag_left; jf.setcursor(new cursor(cursor.w_resize_cursor)); } else if(p.getx()> =(jf.getWidth() - bredth)&& p.gety()<(jf.getheight() - bredth)&& p.gety()> bredth){dragtype = drag_right; jf.setcursor(new cursor(cursor.e_resize_cursor)); } else if(p.getx()<= bredth && p.gety()> =(jf.getheight() - bredth)){dragtype = drag_bottomleft; jf.setcursor(new cursor(cursor.sw_resize_cursor)); } else if(p.getx()> swerdth && p.getx()<(jf.getWidth() - bredth)&& p.gety()> =(jf.getheigh() - bredth){dragtype = drag_bottom; jf.setcursor(new cursor(cursor.s_resize_cursor)); } else if(p.getx()> =(jf.getWidth() - 幅)&& p.gety()> =(jf.getheight() - bredth){dragtype = drag_bottomright; jf.setcursor(new cursor(cursor.se_resize_cursor)); } else {dragtype = drag_move; jf.setcursor(new cursor(cursor.move_cursor)); falseを返します。 } trueを返します。 }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。