使 JInternal Frame 不可移动

Make JInternal Frame non-moveable

我正在 java swing 中使用拖放组件进行练习,我遇到了一个问题,我在 JPanel 中放入了一个 JInternal帧

,可移动,

i want to make it un-moveable

,用的是哪个事件?我该怎么做 ?我在互联网上搜索,但不幸的是无法找到解决方案。

我在网上找到了这个,但我想借助 swing 中的事件选项来完成。

     // Make first internal frame unmovable

JInternalFrame[] frames = frame.desktop.getAllFrames();
JInternalFrame f = frames[0];
BasicInternalFrameUI ui = (BasicInternalFrameUI)f.getUI();

Component north = ui.getNorthPane();
MouseMotionListener[] actions =        // there is no option for MouseMotionListener in the event option
(MouseMotionListener[])north.getListeners(MouseMotionListener.class);

for (int i = 0; i < actions.length; i++)
north.removeMouseMotionListener( actions[i] );

DesktopManager 负责所有内部框架的操作。所以你可以使用 proxy pattern 重新定义它,使你的一些框架不可移动。这是给你的不完整示例(抱歉,我没有测试过这段代码,所以我不确定它是否有效)。

private static class ProxyDesktopManager implements DesktopManager {

    private final DesktopManager delegate;

    public ProxyDesktopManager(DesktopManager delegate) {
        this.delegate = Objects.requireNonNull(delegate);
    }

    // Check whether frame is moveable
    private boolean checkFrameMovable(JComponent frame) {
        if (frame instanceof JInternalFrame) {
            // TODO check whether the frame if movable

        }
        return false;
    }

    @Override
    public void beginDraggingFrame(JComponent f) {
        if (checkFrameMovable(f)) {
            delegate.beginDraggingFrame(f);
        }
    }

    @Override
    public void dragFrame(JComponent f, int newX, int newY) {
        if (checkFrameMovable(f)) {
            delegate.dragFrame(f, newX, newY);
        }
    }

    @Override
    public void endDraggingFrame(JComponent f) {
        if (checkFrameMovable(f)) {
            delegate.endDraggingFrame(f);
        }
    }

    @Override
    public void openFrame(JInternalFrame f) {
        delegate.openFrame(f);
    }

    @Override
    public void closeFrame(JInternalFrame f) {
        delegate.closeFrame(f);
    }

    @Override
    public void maximizeFrame(JInternalFrame f) {
        delegate.maximizeFrame(f);
    }

    @Override
    public void resizeFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) {
        delegate.resizeFrame(f, newX, newY, newWidth, newHeight);
    }

    // IMPORTANT: simply delegate all another methods like openFrame or 
    // resizeFrame
}

现在您可以将此代理 class 用于您的 JDesktopPane

JDesktopPane desktop = new JDesktopPane();
desktop.setDesktopManager(new ProxyDesktopManager(desktop.getDesktopManager()));