JDesktopPane 内のすべての JInternalFrames の z オーダー (層の深さ) を取得するにはどうすればよいでしょうか。これには簡単な方法はないようです。何か案は?
3345 次
1 に答える
4
私はこれを試していませんが、Container
クラス (クラスの祖先JDesktopPane
) にはgetComponentZOrder
メソッドが含まれています。Component
にある aを渡すContainer
と、 の z オーダーが として返されます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);
上記の例では、JDesktopPane
に 3 つの が取り込まれ、3 番目の には、 のリストとその z オーダーをJInternalFrame
に出力するボタンがあります。JInternalFrame
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 に答える