遍历Jframe中的所有对象 [英] Iterate through all objects in Jframe

查看:69
本文介绍了遍历Jframe中的所有对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题.我有一个使用javax.swing.JFrame进行的项目.我想遍历我在Jframe中添加的所有对象.那有可能,我该怎么办?

I have a simple question. I have a project made with javax.swing.JFrame. I would like to iterate through all the objects that i Have added in the Jframe. Is that possible, how can I do it?

推荐答案

这将迭代JFrame的contentPane内部的所有组件,并将它们打印到控制台:

this will iterate through all components inside your JFrame's contentPane and print them to the console:

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());
}

这篇关于遍历Jframe中的所有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆