虽然我还没有尝试过这个,Container http://java.sun.com/javase/6/docs/api/java/awt/Container.html类(它是JDesktopPane http://java.sun.com/javase/6/docs/api/javax/swing/JDesktopPane.html类)包含一个getComponentZOrder http://java.sun.com/javase/6/docs/api/java/awt/Container.html#getComponentZOrder(java.awt.Component)方法。通过通过Component http://java.sun.com/javase/6/docs/api/java/awt/Component.html这是在Container
,它将返回 z 顺序int
. The Component
该方法返回的 z 顺序值最低的则最后绘制,换句话说,绘制在顶部。
耦合与JDesktopPane.getAllFrames http://java.sun.com/javase/6/docs/api/javax/swing/JDesktopPane.html#getAllFrames()方法,它返回一个数组JInternalFrames http://java.sun.com/javase/6/docs/api/javax/swing/JInternalFrame.html,我认为可以获得内部框架的 z 顺序。
Edit
我实际上已经尝试过了,它似乎有效:
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
s,第三个有一个按钮,将输出一个列表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
该示例使用大量匿名内部类只是为了保持代码简短,但实际的程序可能不应该这样做。