java – 如何在JDesktopPane中获取JInternalFrames的z顺序
作者:互联网
如何获得JDesktopPane中所有JInternalFrame的z顺序(层深度).似乎没有直接的方式.有任何想法吗?
解决方法:
虽然我没有尝试过这个,但是Container
类(它是JDesktopPane
类的祖先)包含一个getComponentZOrder
方法.通过传递容器中的Component
,它将返回作为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填充了三个JInternalFrames,第三个JInternalFrames具有一个按钮,该按钮将JInternalFrames列表及其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
该示例使用了许多匿名内部类来保持代码简短,但实际程序可能不应该这样做.
标签:java,swing,jinternalframe 来源: https://codeday.me/bug/20190627/1302313.html