如何将最小的 jinternalframe 保持在顶部 [英] how to keep minimized jinternalframe on top

查看:40
本文介绍了如何将最小的 jinternalframe 保持在顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 frames. please find the code below

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

    }
}

推荐答案

你可以试试:JDesktopPane#setComponentZOrder(Component com, int i).根据文档:

将指定的组件移动到指定的 z-order 索引中容器.z-order 确定组件的顺序绘;具有最高 z-order 的组件首先绘制,然后具有最低 z 顺序绘制的组件最后.哪里组件重叠,具有较低 z 顺序的组件绘制在具有较高 z 阶的组件.

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.

...

注意:

并非所有平台都支持更改 z 顺序重量级组件从一个容器到另一个容器调用 removeNotify.没有办法检测一个平台支持这一点,所以开发者不应该做任何假设.

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.

这将允许您设置 JDesktopPane 中包含的 JInternalFrame 的顺序.

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

更新:

根据我的评论:

据我所知,它是默认行为,似乎不是JDesktopPane#setComponentZOrder(Component com, int i)JInternalFrameiconified 时.它在 正常时工作正常状态

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

解决方案:

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

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

        }
    });

当它最小化时,我们也不能忘记将它添加回DEFAULT_LAYER:

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

        }
    });

完整代码如下:

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

这篇关于如何将最小的 jinternalframe 保持在顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆