4

JDesktopPane 内のすべての JInternalFrames の z オーダー (層の深さ) を取得するにはどうすればよいでしょうか。これには簡単な方法はないようです。何か案は?

4

1 に答える 1

4

私はこれを試していませんが、Containerクラス (クラスの祖先JDesktopPane) にはgetComponentZOrderメソッドが含まれています。Componentにある aを渡すContainerと、 の z オーダーが として返されますint。メソッドによって返される最小のComponentz オーダー値を持つ が最後に描画されます。つまり、上に描画されます。

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に 3 つの が取り込まれ、3 番目の には、 のリストとその z オーダーをJInternalFrameに出力するボタンがあります。JInternalFrameSystem.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 に答える