• 追加された行はこの色です。
  • 削除された行はこの色です。
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) {
   public void mouseDragged(MouseEvent me) {
     JDesktopPane desktop = frame.getDesktopPane();
     Point ep = e.getPoint();
     Point p  = SwingUtilities.convertPoint(frame, ep.x-loc.x, ep.y-loc.y, desktop);
     int x = desktop.getSize().width -frame.getSize().width -p.x;
     int y = desktop.getSize().height-frame.getSize().height-p.y;
     if(Math.abs(p.x)>=d && Math.abs(p.y)>=d && Math.abs(x)>=d && Math.abs(y)>=d) return;
     desktop.getDesktopManager().dragFrame(
       frame,
       (p.x<x)?((Math.abs(p.x)<d)?0:p.x):((Math.abs(x)<d)?x+p.x:p.x),
       (p.y<y)?((Math.abs(p.y)<d)?0:p.y):((Math.abs(y)<d)?y+p.y:p.y));
     Point ep = me.getPoint();
     Point pt = SwingUtilities.convertPoint(frame, ep.x-loc.x, ep.y-loc.y, desktop);
     int e = pt.x;
     int n = pt.y;
     int w = desktop.getSize().width -frame.getSize().width -e;
     int s = desktop.getSize().height-frame.getSize().height-n;
     if(isNear(e) || isNear(n) || isNear(w) || isNear(s)) {
       int x = (e<w)?(isNear(e)?0:e):(isNear(w)?w+e:e);
       int y = (n<s)?(isNear(n)?0:n):(isNear(s)?s+n:n);
       desktop.getDesktopManager().dragFrame(frame,x,y);
     }
   }
   private boolean isNear(int c) {
     return (Math.abs(c)<10);
   }
 }

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

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

- 課題
-- アイコン化している時に吸着できない

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