如何将最小化的jinternalframe保持在最顶层

时间:2013-02-14 11:33:27

标签: java swing layout jinternalframe jdesktoppane

我有四个内部框架和3个按钮。当我点击最大化按钮时,最大化但是它与所有框架重叠。但我的观点是它应该显示最小化的框架。 请找到下面的代码

  package Project;

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) {
                    //Logger.getLogger(MinPanel1.class.getName()).log(Level.SEVERE, null, ex);
                } catch (PropertyVetoException ex) {
                    // Logger.getLogger(MinPanel1.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        });
    }

    private void createAndShowGUI() throws HeadlessException, PropertyVetoException {
        JFrame frame = new JFrame();
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final JDesktopPane jdp = new JDesktopPane() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }
        };

        frame.setContentPane(jdp);
        frame.pack();

        createAndAddInternalFrame(jdp, 0, 0);
        createAndAddInternalFrame(jdp, 200, 0);
        createAndAddInternalFrame(jdp, 400, 0);
        createAndAddInternalFrame(jdp, 600, 0);


        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);
               final JInternalFrame jInternalFrame1 = new JInternalFrame("Test2", true, true, true, true);
JPanel jp= new JPanel();
        JButton jb1 = new JButton("min");
        JButton jb2 = new JButton("[]");
        JButton jb3 = new JButton("X");

        jInternalFrame.setLayout(new GridLayout(2, 2,2,2));
        jInternalFrame1.add(jb1);
        jInternalFrame.setSize(200, 200);//testing
        jInternalFrame.setLayout(new GridLayout(2,2));

        JButton jb= new JButton("min");
       // jInternalFrame.add(jb);
      //  jInternalFrame.add(jb3);
        //jInternalFrame.add(jb2);
        jp.add(jb);
        jp.add(jb2);
        jp.add(jb3);

        jInternalFrame.add(jp);
        jb.addActionListener(new ActionListener()
                {


            @Override
            public void actionPerformed(ActionEvent ae) {
                        try {
                            jInternalFrame.setIcon(true);
                        } catch (PropertyVetoException ex) {
                        }

            }


        });
        jb1.addActionListener(new ActionListener()
                {


            @Override
            public void actionPerformed(ActionEvent ae) {
                        try {
                            jInternalFrame.setIcon(true);
                        } catch (PropertyVetoException ex) {
                        }

            }


        });
        jb2.addActionListener(new ActionListener()
        {


    @Override
    public void actionPerformed(ActionEvent ae) {
        try {
              jInternalFrame.setMaximum(true);

            }
        catch(Exception e)
        {

        }

    }


});jb3.addActionListener(new ActionListener()
{


@Override
public void actionPerformed(ActionEvent ae) {
        try {
            jInternalFrame.dispose();
        } catch (Exception ex) {
        }

}


});

        BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) jInternalFrame.getUI()).getNorthPane();
        jInternalFrame.remove(titlePane);


        jInternalFrame.setVisible(true);
        jInternalFrame1.setVisible(true);

        jdp.add(jInternalFrame);
                //jdp.add(jInternalFrame1);

    }
}

2 个答案:

答案 0 :(得分:5)

您可以尝试:JDesktopPane#setComponentZOrder(Component com, int i)As per docs

  

将指定的组件移动到指定的z-order索引中   容器。 z顺序确定组件的顺序   绘;具有最高z次序涂料的组件首先和   具有最低z次序涂料的组件最后。哪里有组件   重叠,具有较低z次序涂料的组件   具有更高z顺序的组件。

     

...

     

注意:

     

并非所有平台都支持更改z顺序   从一个容器到另一个容器的重量级组件没有   请致电removeNotify。没有办法检测是否有平台   支持这一点,因此开发人员不应做出任何假设。

这样您就可以设置JInternalFrame中包含的JDesktopPane的顺序。

<强>更新

根据我的评论:

  

从我可以看到它的默认行为,似乎不是   过JDesktopPane#setComponentZOrder(Component com, int i)   当JInternalFrame 图标化时它在正常时工作正常   状态

<强>解决方案:

我建议调整显示最大化JInternalFrame的图层:

    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();
            }

        }
    });

我们也必须忘记在最小化时将其添加回DEFAULT_LAYER

    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) {
            }

        }
    });

以下是完整代码:

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);
    }
}

答案 1 :(得分:4)

这是在最大化按钮处理程序中调用moveToBack()的结果。还记得在内部框架上调用pack()

附录:我已更新示例以包含 max min icon 按钮。按钮使用Action以便于测试,内部框架具有不同的名称。请参阅createToolBar()动态更改L&amp; F,例如

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);
    }
}