移動/サイズ変更を担当する共同作業者は、DesktopPaneManager です。したがって、移動をペイン内に制限しようとします。これは、概念の簡単で汚れた証明です。
JDesktopPane background = new JDesktopPane();
JInternalFrame internalFrame = new JInternalFrame("Internal Frame",
true, true, true, true);
DesktopManager manager = new DefaultDesktopManager() {
/** This moves the <code>JComponent</code> and repaints the damaged areas. */
@Override
public void setBoundsForFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) {
boolean didResize = (f.getWidth() != newWidth || f.getHeight() != newHeight);
if (!inBounds((JInternalFrame) f, newX, newY, newWidth, newHeight)) return;
f.setBounds(newX, newY, newWidth, newHeight);
if(didResize) {
f.validate();
}
}
protected boolean inBounds(JInternalFrame f, int newX, int newY, int newWidth, int newHeight) {
if (newX < 0 || newY < 0) return false;
if (newX + newWidth > f.getDesktopPane().getWidth()) return false;
if (newY + newHeight > f.getDesktopPane().getHeight()) return false;
return true;
}
};
background.setDesktopManager(manager);
明らかに解決すべき問題がいくつかあります:-) Fi
- LAF に応じてマネージャーを使用します。これは、インストールされている LAF に他のすべてを委譲するラッパー DesktopManager を実装することで実行できます。
- 副作用をチェックします(壁にぶつかった後、ドラッグが反応しないように見えます。他に必要なものがあるかもしれません)
編集
明確にするために:「応答しない」とは、ユーザーがリリースして再度押したりドラッグしたりして(内部フレームがデスクトップの境界に達したら)、さらに移動する必要があることを意味します。BorderListener (BasicInternalFrame によってインストールされた mouseListener) は最初のプレスに関連する状態を保持し、その最初の位置に関連する再配置を要求するため、これはそれほど驚くべきことではありません。フレームがどこかにスタックした状態でマウスをドラッグすると、その内部状態が混乱します。
面白いことにコードを見ると外側に押し出さないように動きを制限する意図があったようで、
// Make sure we stay in-bounds
if(newX + i.left <= -__x)
newX = -__x - i.left + 1;
if(newY + i.top <= -__y)
newY = -__y - i.top + 1;
if(newX + __x + i.right >= pWidth)
newX = pWidth - __x - i.right - 1;
if(newY + __y + i.bottom >= pHeight)
newY = pHeight - __y - i.bottom - 1;
ただし、これは現在のマウスの位置に相対的です。