问题描述
如何获得JDesktopPane中所有JInternalFrame的z顺序(图层深度)。似乎没有直接的方式。有什么想法吗?
How would one go about getting the z order (layer depth of ) all the JInternalFrames inside a JDesktopPane. There does not seem to be a straight forward way for this. Any ideas?
推荐答案
虽然我没试过这个,但类(它的祖先是 class)包含方法。通过,它位于容器
中,它将返回z-order为 int
。具有该方法返回的最低z顺序值的组件
最后绘制,换句话说,绘制在顶部。
Although I haven't tried this, the Container
class (which is an ancestor of the JDesktopPane
class) contains a getComponentZOrder
method. By passing a Component
which is in the Container
, it will return the z-order of as an int
. The Component
with the lowest z-order value returned by the method is drawn last, in other words, is drawn on top.
与方法,返回,我认为可以获得内部帧的z顺序。
Coupling with the JDesktopPane.getAllFrames
method, which returns an array of JInternalFrames
, I would think that one could obtain the z-order of the internal frames.
编辑
我实际上尝试过它似乎有效:
I've actually tried it out and it seems to work:
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
列表及其z-订购 System.out
。
In the above example, a JDesktopPane
is populated with three JInternalFrame
s with the third one having a button which will output a list of JInternalFrame
s and its z-order to 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
该示例使用了许多匿名内部类来保持代码简短,但实际程序可能不应该这样做。
The example uses a lot of anonymous inner classes just to keep the code short, but an actual program probably should not do that.
这篇关于如何在JDesktopPane中获取JInternalFrame的z顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!