ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 如何在JDesktopPane中获取JInternalFrames的z顺序

2019-06-27 06:48:27  阅读:257  来源: 互联网

标签:java swing jinternalframe


如何获得JDesktopPane中所有JInternalFrame的z顺序(层深度).似乎没有直接的方式.有任何想法吗?

解决方法:

虽然我没有尝试过这个,但是Container类(它是JDesktopPane类的祖先)包含一个getComponentZOrder方法.通过传递容器中的Component,它将返回作为int的z顺序.方法返回的具有最低z顺序值的Component最后绘制,换句话说,绘制在顶部.

JDesktopPane.getAllFrames方法耦合,返回JInternalFrames的数组,我认为可以获得内部帧的z顺序.

编辑

我实际上尝试过它似乎工作:

final JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JDesktopPane desktopPane = new JDesktopPane();
desktopPane.add(new JInternalFrame("1") {
    {
        setVisible(true);
        setSize(100, 100);
    }
});
desktopPane.add(new JInternalFrame("2") {
    {
        setVisible(true);
        setSize(100, 100);
    }
});
desktopPane.add(new JInternalFrame("3") {
    JButton b = new JButton("Get z-order");
    {
        setVisible(true);
        setSize(100, 100);
        getContentPane().add(b);
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                JInternalFrame[] iframes = desktopPane.getAllFrames();
                for (JInternalFrame iframe : iframes)
                {
                    System.out.println(iframe + "\t" +
                            desktopPane.getComponentZOrder(iframe));
                }
            }
        });
    }
});

f.setContentPane(desktopPane);
f.setLocation(100, 100);
f.setSize(400, 400);
f.validate();
f.setVisible(true);

在上面的示例中,JDesktopPane填充了三个JInternalFrames,第三个JInternalFrames具有一个按钮,该按钮将JInternalFrames列表及其z-order输出到System.out.

示例输出如下:

JDesktopPaneTest$3[... tons of info on the frame ...]    0
JDesktopPaneTest$2[... tons of info on the frame ...]    1
JDesktopPaneTest$1[... tons of info on the frame ...]    2

该示例使用了许多匿名内部类来保持代码简短,但实际程序可能不应该这样做.

标签:java,swing,jinternalframe
来源: https://codeday.me/bug/20190627/1302313.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有