• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JDesktopPaneにJInternalFrameを吸着させる
#navi(../)
*JDesktopPaneにJInternalFrameを吸着させる [#t9488112]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2007-01-01~
更新日:&lastmod;

#contents

**概要 [#o18d56cf]
JDesktopPaneとJInternalFrameの距離が近くなった場合、これらを自動的に吸着させます。

#screenshot

**サンプルコード [#o8501b2d]
 private class MagneticListener extends MouseInputAdapter {
   private final JInternalFrame frame;
   private final Point loc = new Point();
   private static final int d = 10;
   public MagneticListener(JInternalFrame frame) {
     this.frame = frame;
   }
   public void mousePressed(MouseEvent e) {
     Point p1 = frame.getLocation();
     Point p2 = SwingUtilities.convertPoint(frame, e.getPoint(), frame.getDesktopPane());
     loc.setLocation(p2.x-p1.x, p2.y-p1.y);
   }
   public void mouseDragged(MouseEvent e) {
     JDesktopPane desktop = frame.getDesktopPane();
     Point p = e.getPoint();
     Point pp = SwingUtilities.convertPoint(frame, p.x-loc.x, p.y-loc.y, desktop);
     int xx = desktop.getSize().width -frame.getSize().width -pp.x;
     int yy = desktop.getSize().height-frame.getSize().height-pp.y;
     if(Math.abs(pp.x)>=d && Math.abs(pp.y)>=d && Math.abs(xx)>=d && Math.abs(yy)>=d) return;
     desktop.getDesktopManager().dragFrame(
       frame,
       (pp.x<xx)?((Math.abs(pp.x)<d)?0:pp.x):((Math.abs(xx)<d)?xx+pp.x:pp.x),
       (pp.y<yy)?((Math.abs(pp.y)<d)?0:pp.y):((Math.abs(yy)<d)?yy+pp.y:pp.y));
   }
 }

-&jnlp;
-&jar;
-&zip;

**解説 [#oa45d781]
上記のサンプルでは、JDesktopPaneとJInternalFrameの距離が10pt以下になった場合、DesktopManager#.dragFrame(JInternalFrame,int,int)メソッドを使ってJInternalFrameの位置を変更することで、吸着を行っています。

//**参考リンク
**コメント [#y9744126]
#comment