如何在jDesktopPane中停止拖动事件

时间:2015-08-29 15:30:56

标签: java netbeans

我想阻止在桌面窗格中拖动jInternalForm。 我试过这里的步骤: Preventing JInternalFrame from being moved out of a JDesktopPane

但它对我不起作用。有人可以为此建议一个有效的覆盖方法。

1 个答案:

答案 0 :(得分:0)

以下是一个更简单的自定义DesktopManager示例,用于将内部框架保持在桌面范围内:

public class BoundsDesktopManager extends DefaultDesktopManager
{
    /*
    **  This is called anytime a frame is moved. 
    **  This implementation keeps the frame from leaving the desktop.
    */
    @Override
    public void dragFrame(JComponent component, int x, int y)
    {
        // Deal only with internal frames

        if (component instanceof JInternalFrame)
        {
            JInternalFrame frame = (JInternalFrame)component;
            JDesktopPane desktop = frame.getDesktopPane();
            Dimension d = desktop.getSize();

            // Too far left or right?

            if (x < 0)
            {
                x = 0; 
            }
            else if (x + frame.getWidth() > d.width)
            {
                x = d.width - frame.getWidth();
            }

            //  Too high or low?

            if (y < 0)
            {
                y = 0;
            }
            else if (y + frame.getHeight() > d.height)
            {
                y = d.height - frame.getHeight();
            }
        }

        // Pass along the (possibly cropped) values to the normal drag handler.

        super.dragFrame(component, x, y);
    }
}