Bor*_*lis 3 swing loops jframe
我有一个简单的问题.我有一个用javax.swing.JFrame制作的项目.我想迭代我在Jframe中添加的所有对象.这可能,我该怎么办?
这将迭代JFrame的contentPane中的所有组件并将它们打印到控制台:
public void listAllComponentsIn(Container parent)
{
for (Component c : parent.getComponents())
{
System.out.println(c.toString());
if (c instanceof Container)
listAllComponentsIn((Container)c);
}
}
public static void main(String[] args)
{
JFrame jframe = new JFrame();
/* ... */
listAllComponentsIn(jframe.getContentPane());
}
Run Code Online (Sandbox Code Playgroud)