How to move minimized components placed at the bottom of the container?

后端 未结 2 1771
臣服心动
臣服心动 2021-01-20 02:01

I have three components in a container and buttons in it. When I hit the minimize button the components gets minimized to the bottom of the container and when I hit the mini

2条回答
  •  一整个雨季
    2021-01-20 02:34

    I've tested this with Metal and Windows L&F, you might need to test it with some others.

    Basically, when the component is invalidated and the doLayout method is called, we check for the existence of any JInternalFrame.JDesktopIcon components. We then take these and layout them out as we like...

    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 icons = new ArrayList(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();
    
                }
            }
        }
    }
    

    Make no mistake, this is a rough hack

    Updated

    int x = 0;
    for (Component icon : icons) {
        int y = getHeight() - icon.getHeight();
        icon.setLocation(x, y);
        x += icon.getWidth();
        setLayer(icon, 10); // <--- Add me
    }
    

    To your other problem, you simply need to move the icon to a higher layer. The problem with this, is you actually need to find a layer high enough. You could use Integer.MAX_VALUE, but that's a little harsh (and you might want something over the top of the that), instead, you could calculate the maximum layer and sit +1 ontop of that...

    public void doLayout() {
        super.doLayout();
        List icons = new ArrayList(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);
    
        }
    }
    

    You really need to take the the time to study How to use Internal Frames and How to use Layered Panes as (at least the last part) is covered in these...

提交回复
热议问题