gpt4 book ai didi

java - 使 JInternal Frame 不可移动

转载 作者:行者123 更新时间:2023-12-02 11:12:46 26 4
gpt4 key购买 nike

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

, 它是可移动的,

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
}

现在您可以为您的 JDesktopPane 使用此代理类

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

关于java - 使 JInternal Frame 不可移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50502065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com