Java 如何移动放置在容器底部的最小化组件?

Java 如何移动放置在容器底部的最小化组件?,java,swing,jinternalframe,mouse-listeners,Java,Swing,Jinternalframe,Mouse Listeners,我在一个容器中有三个组件和按钮。当我点击最小化按钮时,组件被最小化到容器的底部,当我点击最小化组件时,它被最大化 假设三个组件位于底部,如果我最大化第二个组件,那么它会最大化,第三个最小化组件不会占据第二个组件的位置,这仍然是空间 屏幕截图 诸如此类: jdp.setDesktopManager( new DefaultDesktopManager(){ @Override public void deiconifyFrame(JInternalFrame f) {

我在一个容器中有三个组件和按钮。当我点击最小化按钮时,组件被最小化到容器的底部,当我点击最小化组件时,它被最大化

假设三个组件位于底部,如果我最大化第二个组件,那么它会最大化,第三个最小化组件不会占据第二个组件的位置,这仍然是空间

屏幕截图

诸如此类:

jdp.setDesktopManager( new DefaultDesktopManager(){
    @Override
    public void deiconifyFrame(JInternalFrame f) {
        super.deiconifyFrame(f);
        JDesktopPane d = f.getDesktopPane();
         JInternalFrame[] frames = d.getAllFrames();
         for(JInternalFrame frame : frames ) {
             Rectangle bounds = getBoundsForIconOf(frame);
              // relayout all frames
         }
    }
});

我已经用金属和Windows L&F测试过了,你可能需要用其他一些测试

基本上,当组件失效并且调用
doLayout
方法时,我们检查是否存在任何
JInternalFrame.JDesktopIcon
组件。然后我们把它们拿出来,按照我们喜欢的方式布局

public class TestInternalFrame {

    public static void main(String[] args) {
        new TestInternalFrame();
    }

    private int xpos = 0;
    private int ypos = 0;

    public TestInternalFrame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
                DesktopPane pane = new DesktopPane();
                pane.add(newInternalFrame());
                pane.add(newInternalFrame());
                pane.add(newInternalFrame());

                JFrame frame = new JFrame();
                frame.add(pane);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });
    }

    public JInternalFrame newInternalFrame() {
        JInternalFrame inf = new JInternalFrame("Blah", true, true, true, true);
        inf.setLocation(xpos, ypos);
        inf.setSize(100, 100);
        inf.setVisible(true);

        xpos += 50;
        ypos += 50;

        return inf;
    }

    public class DesktopPane extends JDesktopPane {

        @Override
        public void doLayout() {
            super.doLayout();
            List<Component> icons = new ArrayList<Component>(25);
            for (Component comp : getComponents()) {
                if (comp instanceof JInternalFrame.JDesktopIcon) {
                    icons.add(comp);
                }
            }

            int x = 0;
            for (Component icon : icons) {

                int y = getHeight() - icon.getHeight();
                icon.setLocation(x, y);
                x += icon.getWidth();

            }
        }
    }
}

你真的需要花点时间学习,因为(至少最后一部分)在这些内容中有所涉及…

请看@MadProgrammer它与鼠标侦听器链接了吗?我不这么认为对不起,读它为“用户移动”@MadProgrammer对不起,但我没有发现任何与我的问题相关的东西。你能把它分类吗?@ MyDealver不应该考虑使用<代码> GridBagLayout <代码>,然后基于一些计算来减少组件的x坐标吗?这就是我从中得到的:)我喜欢你的想法,但是你可能会在不同平台下改变
JDesktop
的默认行为(我知道,因为Windows实现有一段时间有一个bug,我们不得不换成另一个:P))有趣的回答,我没有know@oliholz对不起,它坏了。您可以尝试其他方法吗?我们花了4周时间试图跟踪此问题(从金属切换到Windows,一切都感觉不一样)-最终依赖于
DefaultDesktopManager
Ithink@oliholz这更像是一个“小心”而不是任何东西,仍然是一个好主意。谢谢你发布你的答案,但它完成了一半的答案。正如我之前所说,当我最大化一帧时,最小化的帧应该是可见的。但是,如果最大帧与所有其他帧重叠(即使它处于原始位置或最小化状态),我希望您理解我的要求谢谢您的帖子。您可能已经看到了我的原始代码,它显示我已经为最大、最小等创建了自己的按钮。但在这段代码中,我们使用的是默认按钮。所以我想知道如果我们创建了自己的按钮,那么它会在这里工作吗?在最大值之后,最小面板会移开吗?如果我理解正确(IIUC),您可以使用
操作
来封装功能,正如所引用的示例中所建议的那样。@sukant只要您使用内部帧的最小/最大/还原功能,您就应该fine@MadProgrammer请看第二张图片,我不想使用内置按钮,如min/max/restore。我想创建自己的按钮,所以我要求这样做
public class TestInternalFrame {

    public static void main(String[] args) {
        new TestInternalFrame();
    }

    private int xpos = 0;
    private int ypos = 0;

    public TestInternalFrame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
                DesktopPane pane = new DesktopPane();
                pane.add(newInternalFrame());
                pane.add(newInternalFrame());
                pane.add(newInternalFrame());

                JFrame frame = new JFrame();
                frame.add(pane);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });
    }

    public JInternalFrame newInternalFrame() {
        JInternalFrame inf = new JInternalFrame("Blah", true, true, true, true);
        inf.setLocation(xpos, ypos);
        inf.setSize(100, 100);
        inf.setVisible(true);

        xpos += 50;
        ypos += 50;

        return inf;
    }

    public class DesktopPane extends JDesktopPane {

        @Override
        public void doLayout() {
            super.doLayout();
            List<Component> icons = new ArrayList<Component>(25);
            for (Component comp : getComponents()) {
                if (comp instanceof JInternalFrame.JDesktopIcon) {
                    icons.add(comp);
                }
            }

            int x = 0;
            for (Component icon : icons) {

                int y = getHeight() - icon.getHeight();
                icon.setLocation(x, y);
                x += icon.getWidth();

            }
        }
    }
}
int x = 0;
for (Component icon : icons) {
    int y = getHeight() - icon.getHeight();
    icon.setLocation(x, y);
    x += icon.getWidth();
    setLayer(icon, 10); // <--- Add me
}
public void doLayout() {
    super.doLayout();
    List<Component> icons = new ArrayList<Component>(25);
    int maxLayer = 0;
    for (Component comp : getComponents()) {
        if (comp instanceof JInternalFrame.JDesktopIcon) {
            icons.add(comp);
            maxLayer = Math.max(getLayer(comp), maxLayer);
        }
    }

    maxLayer++;
    int x = 0;
    for (Component icon : icons) {

        int y = getHeight() - icon.getHeight();
        icon.setLocation(x, y);
        x += icon.getWidth();
        setLayer(icon, maxLayer);

    }
}