如何在JDesktopPane中获取JInternalFrame的z顺序

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

虽然我没有尝试过这个,但Container类(它是JDesktopPane类的祖先)包含一个getComponentZOrder方法。 通过传递Container中的Container ,它将返回作为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填充了三个JInternalFrame ,第三个具有一个按钮,该按钮将输出JInternalFrame列表及其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 

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