how to keep minimized jinternalframe on top

前端 未结 2 1354
时光说笑
时光说笑 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: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);
        }
    }
    

提交回复
热议问题