how to keep minimized jinternalframe on top

前端 未结 2 1351
时光说笑
时光说笑 2020-12-11 06:21

i have four internal frames and 3 buttons in it .When i hit the maximize button,maximizes but it overlaps all the frames.But my point is that it should show the minimized fr

相关标签:
2条回答
  • 2020-12-11 06:42

    You could try: JDesktopPane#setComponentZOrder(Component com, int i). As per docs:

    Moves the specified component to the specified z-order index in the container. The z-order determines the order that components are painted; the component with the highest z-order paints first and the component with the lowest z-order paints last. Where components overlap, the component with the lower z-order paints over the component with the higher z-order.

    ...

    Note:

    Not all platforms support changing the z-order of heavyweight components from one container into another without the call to removeNotify. There is no way to detect whether a platform supports this, so developers shouldn't make any assumptions.

    This will allow you to set the order of the JInternalFrames contained within JDesktopPane.

    UPDATE:

    As per my comment:

    From what I can see its the default behavior and doesnt seem to be over com-able by JDesktopPane#setComponentZOrder(Component com, int i) when the JInternalFrame is iconified. it works fine when its in normal state

    Solution:

    I suggest adjusting the layer on which maximized JInternalFrame is shown:

        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    if (jInternalFrame.isMaximum()) {//restore
                        jInternalFrame.pack();
                    } else {//maximize
                        jInternalFrame.setMaximum(true);
                    }
                    jdp.remove(jInternalFrame);
                    jdp.add(jInternalFrame, JDesktopPane.FRAME_CONTENT_LAYER);
                    jdp.revalidate();
                    jdp.repaint();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
            }
        });
    

    We must also not forget to add it back to the DEFAULT_LAYER when it is minimized:

        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    if (jInternalFrame.getLayer() == JDesktopPane.FRAME_CONTENT_LAYER) {
                        jdp.remove(jInternalFrame);
                        jdp.add(jInternalFrame, JDesktopPane.DEFAULT_LAYER);
                        jdp.revalidate();
                        jdp.repaint();
                    }
                    jInternalFrame.pack();
                    jInternalFrame.setIcon(true);
                } catch (PropertyVetoException ex) {
                }
    
            }
        });
    

    Here is full code:

    enter image description here

    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.HeadlessException;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.PropertyVetoException;
    import javax.swing.JButton;
    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import javax.swing.JInternalFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
    import javax.swing.plaf.basic.BasicInternalFrameUI;
    
    public class Test {
    
        public Test() throws HeadlessException, PropertyVetoException {
            createAndShowGUI();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        new Test();
                    } catch (HeadlessException ex) {
                        ex.printStackTrace();
                    } catch (PropertyVetoException ex) {
                        ex.printStackTrace();
                    }
    
                }
            });
        }
    
        private void createAndShowGUI() throws HeadlessException, PropertyVetoException {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            final JDesktopPane jdp = new JDesktopPane() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(600, 400);
                }
            };
    
            frame.setContentPane(jdp);
            frame.pack();
    
            createAndAddInternalFrame(jdp, 0, 0);
            createAndAddInternalFrame(jdp, 300, 0);
            createAndAddInternalFrame(jdp, 0, 200);
    
            frame.setVisible(true);
        }
    
        private void createAndAddInternalFrame(final JDesktopPane jdp, int x, int y) throws PropertyVetoException {
            final JInternalFrame jInternalFrame = new JInternalFrame("Test1", true, true, true, true);
            jInternalFrame.setLocation(x, y);
    
            JPanel jp = new JPanel();
    
            JButton jb = new JButton("min");
            JButton jb2 = new JButton("max/restore");
            JButton jb3 = new JButton("close");
    
            jInternalFrame.setLayout(new GridLayout(2, 2));
    
            jp.add(jb);
            jp.add(jb2);
            jp.add(jb3);
    
            jInternalFrame.add(jp);
    
            jb.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    try {
                        if (jInternalFrame.getLayer() == JDesktopPane.FRAME_CONTENT_LAYER) {
                            jdp.remove(jInternalFrame);
                            jdp.add(jInternalFrame, JDesktopPane.DEFAULT_LAYER);
                            jdp.revalidate();
                            jdp.repaint();
                        }
                        jInternalFrame.pack();
                        jInternalFrame.setIcon(true);
                    } catch (PropertyVetoException ex) {
                        ex.printStackTrace();
                    }
    
                }
            });
            jb2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    try {
                        if (jInternalFrame.isMaximum()) {//restore
                            jInternalFrame.pack();
                        } else {//maximize
                            jInternalFrame.setMaximum(true);
                        }
                        jdp.remove(jInternalFrame);
                        jdp.add(jInternalFrame, JDesktopPane.FRAME_CONTENT_LAYER);
                        jdp.revalidate();
                        jdp.repaint();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
                }
            });
            jb3.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    try {
                        jInternalFrame.dispose();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
    
                }
            });
    
            BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) jInternalFrame.getUI()).getNorthPane();
            jInternalFrame.remove(titlePane);
    
            jInternalFrame.pack();
            jInternalFrame.setVisible(true);
    
            jdp.add(jInternalFrame);
        }
    }
    
    0 讨论(0)
  • 2020-12-11 06:44

    Here's the result of calling moveToBack() in the maximize button handler. Also remember to call pack() on the internal frame.

    Addendum: I've updated the example to include max, min and icon buttons. The buttons use Action for easier testing, and the internal frames have distinct names. See createToolBar() to change the L&F dynamically, e.g.

    frame.add(createToolBar(frame), BorderLayout.NORTH);
    

    image

    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.beans.PropertyVetoException;
    import javax.swing.AbstractAction;
    import javax.swing.JButton;
    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import javax.swing.JInternalFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    //* @see https://stackoverflow.com/a/14874924/230513 */
    public class Test {
    
        public Test() {
            createAndShowGUI();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new Test();
                }
            });
        }
    
        private void createAndShowGUI() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            final JDesktopPane jdp = new JDesktopPane() {
    
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(600, 400);
                }
            };
            for (int i = 0; i < 4; i++) {
                createInternalFrame(jdp, 100 * i, 100 * i);
            }
            frame.add(jdp);
            frame.pack();
            frame.setVisible(true);
        }
    
        private void createInternalFrame(final JDesktopPane jdp, int x, int y) {
            final JInternalFrame jif = new JInternalFrame("Test" + x, true, true, true, true);
            jif.setLocation(x, y);
            JPanel jp = new JPanel();
            jp.add(new JButton(new AbstractAction("max") {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    try {
                        jif.setMaximum(true);
                        jif.moveToBack();
                    } catch (PropertyVetoException e) {
                        e.printStackTrace();
                    }
    
                }
            }));
            jp.add(new JButton(new AbstractAction("min") {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    try {
                        jif.setMaximum(false);
                    } catch (PropertyVetoException e) {
                        e.printStackTrace();
                    }
    
                }
            }));
            jp.add(new JButton(new AbstractAction("icon") {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    try {
                        jif.setIcon(true);
                    } catch (PropertyVetoException e) {
                        e.printStackTrace();
                    }
                }
            }));
            jif.add(jp);
            jif.pack();
            jif.setVisible(true);
            jdp.add(jif);
        }
    }
    
    0 讨论(0)
提交回复
热议问题