简体   繁体   English

如何使JDesktopPane中的JInternalFrame不重叠

[英]How to make JInternalFrame(s) in JDesktopPane non-overlapping

I have multiple JInternalFrame(s) inside a JDesktopPane. 我在JDesktopPane中有多个JInternalFrame。 All the JInternalFrame(s) are undecorated and I manage the dragging using mouse listeners. 所有JInternalFrame都未修饰,我使用鼠标侦听器管理拖动。 However, I want the internal frames to be non overlapping, ie, one internal frame should not intersect with another. 但是,我希望内部框架不重叠,即一个内部框架不应与另一内部框架相交。

Till now, I can check if two internal frames are overlapping by using getBounds().intersects() method. 到现在为止,我可以使用getBounds()。intersects()方法检查两个内部框架是否重叠。 However, I am clueless as to what should I do next. 但是,我不知道下一步该怎么做。

I tried Google but got no satisfactory answer. 我尝试了Google,但没有得到满意的答案。 Please suggest a solution. 请提出解决方案。

Thank you ! 谢谢 !

Two answers: 两个答案:

  1. If you want to set which internal frame overlaps the other, use the setComponentZOrder(Component comp, int index) function. 如果要设置哪个内部框架与另一个内部框架重叠,请使用setComponentZOrder(Component comp,int index)函数。

eg 例如

JDesktopPane desktopPane = new JDesktopPane();
contentPane.add(desktopPane);
JInternalFrame internalFrame = new JInternalFrame("New JInternalFrame");
desktopPane.add(internalFrame);
desktopPane.setComponentZOrder(internalFrame, 0);

Place the setComponentZOrder function in the JInternalFrame component listener. 将setComponentZOrder函数放置在JInternalFrame组件侦听器中。

    internalFrame.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentMoved(ComponentEvent e) {
            desktopPane.setComponentZOrder(internalFrame, 0);
        }
    });

2. If you want them to strictly not overlap, place them in separate jframes. 2.如果您希望它们严格不重叠,请将它们放在单独的jframe中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM