我有一个简单的问题。我有一个用 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());
}
关于swing - 遍历 Jframe 中的所有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10271116/