如何在 JDesktopPane 中获取所有 JInternalFrames 的 z 顺序(层深度)。似乎没有直接的方法。有任何想法吗?
Nahir
问问题
3345 次
1 回答
4
虽然我没有尝试过,但Container
该类(它是该类的祖先JDesktopPane
)包含一个getComponentZOrder
方法。通过传递Component
中的 a Container
,它将返回 z 顺序作为 a int
。由该方法返回的Component
具有最低 z 顺序值的最后绘制,换句话说,绘制在顶部。
再加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);
在上面的示例中,aJDesktopPane
填充了三个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
该示例使用大量匿名内部类只是为了保持代码简短,但实际程序可能不应该这样做。
于 2009-03-09T03:27:40.510 回答