Swing/MagneticFrame のバックアップの現在との差分(No.7)
TITLE:JDesktopPaneにJInternalFrameを吸着させる
Posted by terai at 2007-01-01
JDesktopPaneにJInternalFrameを吸着させる
JDesktopPaneとJInternalFrameの距離が近くなった場合、これらを自動的に吸着させます。- category: swing folder: MagneticFrame title: JDesktopPaneにJInternalFrameを吸着させる tags: [DesktopManager, JDesktopPane, JInternalFrame] author: aterai pubdate: 2007-01-01T08:38:12+09:00 description: JDesktopPaneとJInternalFrameの距離が近くなった場合、これらを自動的に吸着させます。 image:
概要
JDesktopPane
とJInternalFrame
の距離が近くなった場合、これらを自動的に吸着させます。
- &jnlp;
- &jar;
- &zip;
Screenshot
Advertisement
#screenshot
サンプルコード
#spanend
#spanadd
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
#spanend
#spanadd
desktop.setDesktopManager(new MagneticDesktopManager());
#spanend
#spandel
**サンプルコード [#o8501b2d]
#spanend
#spandel
#code{{
#spanend
#spandel
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
#spanend
#spandel
desktop.setDesktopManager(new DefaultDesktopManager() {
#spanend
@override
public void dragFrame(JComponent frame, int x, int y) {
int e = x; int n = 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)) {
x = (e<w)?(isNear(e)?0:e):(isNear(w)?w+e:e);
y = (n<s)?(isNear(n)?0:n):(isNear(s)?s+n:n);
#spanadd
// ...
#spanend
#spanadd
class MagneticDesktopManager extends DefaultDesktopManager {
#spanend
@Override public void dragFrame(JComponent frame, int x, int y) {
Container c = SwingUtilities.getAncestorOfClass(JDesktopPane.class, frame);
if (c instanceof JDesktopPane) {
JDesktopPane desktop = (JDesktopPane) c;
int e = x;
int n = 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)) {
super.dragFrame(frame, getX(e, w), getY(n, s));
} else {
super.dragFrame(frame, x, y);
}
}
super.dragFrame(frame, x, y);
}
private boolean isNear(int c) {
return (Math.abs(c)<10);
#spanadd
#spanend
private static int getX(int e, int w) {
return e < w ? isNear(e) ? 0 : e : isNear(w) ? w + e : e;
}
#spandel
});
#spanend
#spanadd
#spanend
private static int getY(int n, int s) {
return n < s ? isNear(n) ? 0 : n : isNear(s) ? s + n : n;
}
#spanadd
#spanend
private static boolean isNear(int c) {
return Math.abs(c) < 10;
}
#spanadd
}
#spanend
View in GitHub: Java, Kotlin解説
DesktopManager#dragFrame(JInternalFrame,int,int)メソッドをオーバーライドすることでJInternalFrameの位置を調整しています。上記のサンプルでは、JDesktopPaneとJInternalFrameの距離が10pt以下になった場合、それぞれ吸着するよう設定しています。解説
DesktopManager#dragFrame(JInternalFrame,int,int)
メソッドをオーバーライドすることでJInternalFrame
の配置座標を調整しています。上記のサンプルでは、JDesktopPane
とJInternalFrame
の距離が10px
以下になった場合、それぞれの辺が吸着するよう設定しています。