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

标签 java swing jinternalframe

如何获取 JDesktopPane 中所有 JInternalFrames 的 z 顺序(层深度)。这似乎没有直接的方法。有什么想法吗?

最佳答案

虽然我还没有尝试过,Container类(它是 JDesktopPane 类的祖先)包含一个 getComponentZOrder方法。通过传递 Component它在 Container 中,它将以 int 的形式返回 z 顺序。该方法返回的具有最低 z-order 值的 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 由三个 JInternalFrame 填充,第三个具有一个按钮,该按钮将输出 JInternalFrame 的列表>s 及其 z 顺序到 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 - 如何在 JDesktopPane 中获取 JInternalFrames 的 z 顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/624885/

相关文章:

java - 在Java中使用EventQueue.push()方法时出现"Not applicable for the arguments (void)"错误

Java - 调整 JInternalFrame 大小时出现问题

java - 如何使用 jsoup 从网页中的所有段落中提取完整 URL

java - 获取更多 SSL 调试信息

java - 如何更改 JComboBox 中的箭头样式

java - 如何将 JInternalFrame 添加到 JFrame 中?

java - 缩放和缩放

java - SSL 和验证用户

java - 在 Java 编码中使用 @XmlAnyElement

java - JPanel 类未添加到 JFrame